What is react-native-image-picker?
The react-native-image-picker package allows React Native developers to access the device's camera and photo library. It provides a simple way to capture images and videos or select them from the device's gallery.
What are react-native-image-picker's main functionalities?
Launch Camera
This feature allows you to launch the device's camera to capture a photo. The `launchCamera` function takes an options object to specify the media type and camera type, and a callback function to handle the response.
import { launchCamera } from 'react-native-image-picker';
const options = {
mediaType: 'photo',
cameraType: 'back',
};
launchCamera(options, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
const source = { uri: response.uri };
console.log('Image URI: ', source.uri);
}
});
Launch Image Library
This feature allows you to open the device's image library to select a photo. The `launchImageLibrary` function takes an options object to specify the media type and a callback function to handle the response.
import { launchImageLibrary } from 'react-native-image-picker';
const options = {
mediaType: 'photo',
};
launchImageLibrary(options, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
const source = { uri: response.uri };
console.log('Image URI: ', source.uri);
}
});
Video Recording
This feature allows you to launch the device's camera to record a video. The `launchCamera` function takes an options object to specify the media type as video and the camera type, and a callback function to handle the response.
import { launchCamera } from 'react-native-image-picker';
const options = {
mediaType: 'video',
cameraType: 'back',
};
launchCamera(options, (response) => {
if (response.didCancel) {
console.log('User cancelled video recording');
} else if (response.error) {
console.log('VideoPicker Error: ', response.error);
} else {
const source = { uri: response.uri };
console.log('Video URI: ', source.uri);
}
});
Other packages similar to react-native-image-picker
react-native-camera
The react-native-camera package provides a comprehensive camera module for React Native. It supports photo and video capture, barcode scanning, and text recognition. Compared to react-native-image-picker, it offers more advanced camera functionalities but requires more setup.
expo-image-picker
The expo-image-picker package is part of the Expo ecosystem and provides similar functionalities to react-native-image-picker, such as selecting images and videos from the device's library or capturing them using the camera. It is easier to set up within an Expo project but may not offer as much flexibility as react-native-image-picker in a bare React Native project.
react-native-image-crop-picker
The react-native-image-crop-picker package offers image and video picking with cropping functionality. It provides a more customizable user interface for cropping images compared to react-native-image-picker, making it a good choice if cropping is a required feature.
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:
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,
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) => {
});