CameraRoll #

CameraRoll provides access to the local camera roll / gallery. Before using this you must link the RCTCameraRoll library. You can refer to Linking for help.

Permissions #

The user's permission is required in order to access the Camera Roll on devices running iOS 10 or later. Fill out the NSCameraUsageDescription key in your Info.plist with a string that describes how your app will use this data. This key will appear as Privacy - Camera Usage Description in Xcode.

Methods #

static saveImageWithTag(tag) #

static saveToCameraRoll(tag, type?) #

Saves the photo or video to the camera roll / gallery.

On Android, the tag must be a local image or video URI, such as "file:///sdcard/img.png".

On iOS, the tag can be any image URI (including local, remote asset-library and base64 data URIs) or a local video file URI (remote or data URIs are not supported for saving video at this time).

If the tag has a file extension of .mov or .mp4, it will be inferred as a video. Otherwise it will be treated as a photo. To override the automatic choice, you can pass an optional type parameter that must be one of 'photo' or 'video'.

Returns a Promise which will resolve with the new URI.

static getPhotos(params) #

Returns a Promise with photo identifier objects from the local camera roll of the device matching shape defined by getPhotosReturnChecker.

@param {object} params See getPhotosParamChecker.

Returns a Promise which when resolved will be of shape getPhotosReturnChecker.

You can edit the content above on GitHub and send us a pull request!

Examples #

Edit on GitHub
'use strict'; const React = require('react'); const ReactNative = require('react-native'); const { CameraRoll, Image, Slider, StyleSheet, Switch, Text, View, TouchableOpacity } = ReactNative; const invariant = require('fbjs/lib/invariant'); const CameraRollView = require('./CameraRollView'); const AssetScaledImageExampleView = require('./AssetScaledImageExample'); class CameraRollExample extends React.Component { state = { groupTypes: 'SavedPhotos', sliderValue: 1, bigImages: true, }; _cameraRollView: ?CameraRollView; render() { return ( <View> <Switch onValueChange={this._onSwitchChange} value={this.state.bigImages} /> <Text>{(this.state.bigImages ? 'Big' : 'Small') + ' Images'}</Text> <Slider value={this.state.sliderValue} onValueChange={this._onSliderChange} /> <Text>{'Group Type: ' + this.state.groupTypes}</Text> <CameraRollView ref={(ref) => { this._cameraRollView = ref; }} batchSize={20} groupTypes={this.state.groupTypes} renderImage={this._renderImage} /> </View> ); } loadAsset = (asset) => { if (this.props.navigator) { this.props.navigator.push({ title: 'Camera Roll Image', component: AssetScaledImageExampleView, backButtonTitle: 'Back', passProps: { asset: asset }, }); } }; _renderImage = (asset) => { const imageSize = this.state.bigImages ? 150 : 75; const imageStyle = [styles.image, {width: imageSize, height: imageSize}]; const {location} = asset.node; const locationStr = location ? JSON.stringify(location) : 'Unknown location'; return ( <TouchableOpacity key={asset} onPress={ this.loadAsset.bind( this, asset ) }> <View style={styles.row}> <Image source={asset.node.image} style={imageStyle} /> <View style={styles.info}> <Text style={styles.url}>{asset.node.image.uri}</Text> <Text>{locationStr}</Text> <Text>{asset.node.group_name}</Text> <Text>{new Date(asset.node.timestamp).toString()}</Text> </View> </View> </TouchableOpacity> ); }; _onSliderChange = (value) => { const options = CameraRoll.GroupTypesOptions; const index = Math.floor(value * options.length * 0.99); const groupTypes = options[index]; if (groupTypes !== this.state.groupTypes) { this.setState({groupTypes: groupTypes}); } }; _onSwitchChange = (value) => { invariant(this._cameraRollView, 'ref should be set'); this._cameraRollView.rendererChanged(); this.setState({ bigImages: value }); }; } const styles = StyleSheet.create({ row: { flexDirection: 'row', flex: 1, }, url: { fontSize: 9, marginBottom: 14, }, image: { margin: 4, }, info: { flex: 1, }, }); exports.title = 'Camera Roll'; exports.description = 'Example component that uses CameraRoll to list user\'s photos'; exports.examples = [ { title: 'Photos', render(): React.Element<any> { return <CameraRollExample />; } } ];