react-native-image-picker
A React Native module that allows you to use the native UIImagePickerController UI to either select a photo from the device library or directly from the camera, like so:
![Screenshot of the UIActionSheet](https://github.com/marcshilling/react-native-image-picker/blob/master/AlertSheetImage.jpg)
Requires iOS 8 or higher
Install
npm install react-native-image-picker@latest --save
- In the XCode's "Project navigator", right click on your project's Libraries folder ➜
Add Files to <...>
- Go to
node_modules
➜ react-native-image-picker
➜ select the UIImagePickerManager
folder Make sure you have 'Create Groups' selected - Make sure
UIImagePickerManager.m
is listed under 'Compile Sources' in your project's 'Build Phases' tab - Compile and have fun!
Usage
- In your React Native javascript code, bring in the native module:
var UIImagePickerManager = require('NativeModules').UIImagePickerManager;
- Use it like so:
When you want to display the picker:
var options = {
title: 'Select Avatar',
cancelButtonTitle: 'Cancel',
takePhotoButtonTitle: 'Take Photo...',
chooseFromLibraryButtonTitle: 'Choose from Library...',
customButtons: {
'Choose Photo from Facebook': 'fb',
},
maxWidth: 100,
maxHeight: 100,
quality: 0.2,
allowsEditing: false,
noData: false,
storageOptions: {
skipBackup: true,
path: 'images'
}
};
UIImagePickerManager.showImagePicker(options, (didCancel, response) => {
console.log('Response = ', response);
if (didCancel) {
console.log('User cancelled image picker');
}
else {
if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
}
else {
const source = {uri: 'data:image/jpeg;base64,' + response.data, isStatic: true};
const source = {uri: response.uri.replace('file://', ''), isStatic: true};
this.setState({
avatarSource: source
});
}
}
});
Then later, if you want to display this image in your render() method:
<Image source={this.state.avatarSource} style={styles.uploadAvatar} />
Directly Launching the Camera or Image Library
To Launch the Camera or Image Library directly (skipping the alert dialog) you can
do the following:
UIImagePickerManager.launchCamera(options, (didCancel, response) => {
});
UIImagePickerManager.launchImageLibrary(options, (didCancel, response) => {
});