/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, {Component} from 'react';
import {
Alert,
Image,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import ImagePicker from 'react-native-image-crop-picker';
import Video from 'react-native-video';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
button: {
backgroundColor: 'blue',
marginBottom: 10,
},
text: {
color: 'white',
fontSize: 20,
textAlign: 'center',
},
});
interface ImageState {
uri: string;
width: number;
height: number;
mime: string;
}
interface AppState {
image: ImageState | null;
images: ImageState[] | null;
}
export default class App extends Component<{}, AppState> {
constructor(props: {}) {
super(props);
this.state = {
image: null,
images: null,
};
}
pickSingleWithCamera(
cropping: boolean,
mediaType: 'photo' | 'video' = 'photo',
) {
ImagePicker.openCamera({
cropping: cropping,
width: 500,
height: 500,
includeExif: true,
mediaType,
})
.then(image => {
this.setState({
image: {
uri: image.path,
width: image.width,
height: image.height,
mime: image.mime,
},
images: null,
});
})
.catch(e => Alert.alert('Error', e.message));
}
pickSingleBase64(cropit: boolean) {
ImagePicker.openPicker({
width: 300,
height: 300,
cropping: cropit,
includeBase64: true,
includeExif: true,
})
.then(image => {
console.log('received base64 image');
this.setState({
image: {
uri: `data:${image.mime};base64,` + (image as any).data,
width: image.width,
height: image.height,
mime: image.mime,
},
images: null,
});
})
.catch(e => Alert.alert('Error', e.message));
}
cleanupImages() {
ImagePicker.clean()
.then(() => {
console.log('removed tmp images from tmp directory');
})
.catch(e => {
Alert.alert('Error', e.message);
});
}
cleanupSingleImage() {
let image =
this.state.image ||
(this.state.images && this.state.images.length
? this.state.images[0]
: null);
console.log('will cleanup image', image);
ImagePicker.cleanSingle(image?.uri || '')
.then(() => {
console.log(`removed tmp image ${image?.uri} from tmp directory`);
})
.catch(e => {
Alert.alert('Error', e.message);
});
}
cropLast() {
if (!this.state.image) {
return Alert.alert(
'No image',
'Before open cropping only, please select image',
);
}
ImagePicker.openCropper({
path: this.state.image.uri,
width: 200,
height: 200,
mediaType: 'photo',
})
.then(image => {
console.log('received cropped image', image);
this.setState({
image: {
uri: image.path,
width: image.width,
height: image.height,
mime: image.mime,
},
images: null,
});
})
.catch(e => {
console.log(e);
Alert.alert('Error', e.message);
});
}
pickSingle(cropit: boolean, circular: boolean = false) {
ImagePicker.openPicker({
width: 500,
height: 500,
cropping: cropit,
cropperCircleOverlay: circular,
sortOrder: 'none',
compressImageMaxWidth: 1000,
compressImageMaxHeight: 1000,
compressImageQuality: 1,
compressVideoPreset: 'MediumQuality',
includeExif: true,
cropperStatusBarColor: 'white',
cropperToolbarColor: 'white',
cropperActiveWidgetColor: 'white',
cropperToolbarWidgetColor: '#3498DB',
})
.then(image => {
console.log('received image', image);
this.setState({
image: {
uri: image.path,
width: image.width,
height: image.height,
mime: image.mime,
},
images: null,
});
})
.catch(e => {
console.log(e);
Alert.alert('Error', e.message);
});
}
pickMultiple() {
ImagePicker.openPicker({
multiple: true,
waitAnimationEnd: false,
sortOrder: 'desc',
includeExif: true,
forceJpg: true,
})
.then(images => {
this.setState({
image: null,
images: images.map(i => {
console.log('received image', i);
return {
uri: i.path,
width: i.width,
height: i.height,
mime: i.mime,
};
}),
});
})
.catch(e => Alert.alert('Error', e.message));
}
scaledHeight(oldW: number, oldH: number, newW: number) {
return (oldH / oldW) * newW;
}
renderVideo(video: ImageState) {
console.log('rendering video');
return (
);
}
renderImage(image: ImageState) {
return (
);
}
renderAsset(image: ImageState) {
if (image.mime && image.mime.toLowerCase().indexOf('video/') !== -1) {
return this.renderVideo(image);
}
return this.renderImage(image);
}
render() {
return (
{this.state.image ? this.renderAsset(this.state.image) : null}
{this.state.images
? this.state.images.map(i => (
{this.renderAsset(i)}
))
: null}
this.pickSingleWithCamera(false)}
style={styles.button}>
Select Single Image With Camera
this.pickSingleWithCamera(false, 'video')}
style={styles.button}>
Select Single Video With Camera
this.pickSingleWithCamera(true)}
style={styles.button}>
Select Single With Camera With Cropping
this.pickSingle(false)}
style={styles.button}>
Select Single
this.cropLast()} style={styles.button}>
Crop Last Selected Image
this.pickSingleBase64(false)}
style={styles.button}>
Select Single Returning Base64
this.pickSingle(true)}
style={styles.button}>
Select Single With Cropping
this.pickSingle(true, true)}
style={styles.button}>
Select Single With Circular Cropping
Select Multiple
Cleanup All Images
Cleanup Single Image
);
}
}