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:
Install
npm install react-native-image-picker@latest --save
- In the XCode's "Project navigator", right click on project's name ➜
Add Files to <...>
- Go to
node_modules
➜ react-native-image-picker
➜ add UIImagePickerManager.h
and UIImagePickerManager.m
files - 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:
UIImagePickerManager.showImagePicker('Select Avatar', (isData, response) => {
var source;
if (isData) {
source = {uri: 'data:image/jpeg;base64,' + response, isStatic: true};
} else {
source = {uri: response};
}
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} />