122 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-05-18 12:07:36 +02:00
import React, {Component} from 'react';
import {View, Text, StyleSheet, ScrollView, Image, TouchableOpacity} from 'react-native';
2016-07-27 12:05:06 +02:00
import {NativeModules, Dimensions} from 'react-native';
2016-05-18 12:07:36 +02:00
var ImagePicker = NativeModules.ImageCropPicker;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
button: {
backgroundColor: 'blue',
marginBottom: 10
},
text: {
color: 'white',
fontSize: 20
}
});
export default class App extends Component {
constructor() {
super();
this.state = {
image: null,
images: null
};
}
2016-08-11 20:00:59 +02:00
pickSingleWithCamera(cropping) {
ImagePicker.openCamera({
cropping,
width: 500,
height: 500
}).then(image => {
console.log('received image', image);
this.setState({
image: {uri: image.path, width: image.width, height: image.height},
images: null
});
}).catch(e => alert(e));
}
pickSingleBase64(cropit) {
ImagePicker.openPicker({
width: 300,
height: 300,
cropping: cropit,
includeBase64: true
}).then(image => {
console.log('received image', image);
this.setState({
image: {uri: `data:${image.mime};base64,`+ image.data, width: image.width, height: image.height},
images: null
});
}).catch(e => {});
}
2016-05-18 12:07:36 +02:00
pickSingle(cropit) {
ImagePicker.openPicker({
width: 300,
height: 300,
cropping: cropit
}).then(image => {
console.log('received image', image);
2016-05-18 12:07:36 +02:00
this.setState({
image: {uri: image.path, width: image.width, height: image.height},
images: null
});
}).catch(e => {});
}
pickMultiple() {
ImagePicker.openPicker({
multiple: true
}).then(images => {
this.setState({
image: null,
images: images.map(i => {
console.log('received image', i);
2016-05-18 12:07:36 +02:00
return {uri: i.path, width: i.width, height: i.height};
})
});
}).catch(e => {});
}
scaledHeight(oldW, oldH, newW) {
return (oldH / oldW) * newW;
}
render() {
return <View style={styles.container}>
<ScrollView>
2016-07-27 12:05:06 +02:00
{this.state.image ? <Image style={{width: 300, height: 300, resizeMode: 'contain'}} source={this.state.image} /> : null}
2016-05-18 12:07:36 +02:00
{this.state.images ? this.state.images.map(i => <Image key={i.uri} style={{width: 300, height: this.scaledHeight(i.width, i.height, 300)}} source={i} />) : null}
</ScrollView>
2016-08-11 20:00:59 +02:00
<TouchableOpacity onPress={() => this.pickSingleWithCamera(false)} style={styles.button}>
<Text style={styles.text}>Select Single With Camera</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.pickSingleWithCamera(true)} style={styles.button}>
<Text style={styles.text}>Select Single With Camera With Cropping</Text>
</TouchableOpacity>
2016-05-18 12:07:36 +02:00
<TouchableOpacity onPress={() => this.pickSingle(false)} style={styles.button}>
<Text style={styles.text}>Select Single</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.pickSingleBase64(false)} style={styles.button}>
<Text style={styles.text}>Select Single Returning Base64</Text>
</TouchableOpacity>
2016-05-18 12:07:36 +02:00
<TouchableOpacity onPress={() => this.pickSingle(true)} style={styles.button}>
<Text style={styles.text}>Select Single With Cropping</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.pickMultiple.bind(this)} style={styles.button}>
<Text style={styles.text}>Select Multiple</Text>
</TouchableOpacity>
</View>;
}
}