The Cameraroll module in React-native provides the ability to access local albums.
In the react version of 0.23.0, Android is not supported, and the use of Cameraroll in iOS requires us to do it manually:
Ios:
- Add Rctcameraroll.xcodeproj to our project: Expand Project > Libraies Right-libraies click "Add Files to ' project name '" To locate the project folder/node_modules/ React-native/libraries/cameraroll/rctcameraroll.xcodeproj, added to the project.
- We're going to add LIBRCTCAMERAROLL.A to the link Binary with libraries of the main project, such as:
With this addition, we can use the Cameraroll function in the project without error.
There are two functions in the Cameraroll module: Saveimagewithtag (), Getphotos ().
Saveimagewithtag ()
Save an image to an album.
@param {string} tag on Android, this parameter is a local URI, for example "file:///sdcard/img.png"
.
On an iOS device, it may be one of the following:
- Local URI
- Tags for the resource pool
- Not more than two types, which means that the picture data will be stored in memory (and will always occupy memory while the process continues).
Returns a promise that returns a new URI when the operation succeeds.
Example:
Cameraroll.saveimagewithtag (image). Then (function (Success) {Alert.alert ("', 'Save to album success', [{text:'Determine', onpress: () =Console.log (Success)}] )}, function (Error) {Alert.alert ("', 'failed to save to album', [{text:'Determine', onpress: () =Console.log (Error)}] ) } )}
get the photos in the album. Getphotos ()
Learning this feature is learned in the official demo, it is written in a can use JS file camerarollview.js, we need to introduce this file into our project.
Accessories: Camerarollview.js
File Open will have an error display, but the syntax is not the same, this does not affect the effect.
How to use:
Import Camerarollview from '.. /camerarollview'; exportdefault classCamerarollview extends Component {constructor (props) {super (props); Cameraroll.getphotos ({first: +, Assettype:'Photos'}). Then (function (data) {}, function (Error) {})} _renderimage (asset) {varWindowSize = require ('Dimensions').Get('window'); return ( <touchableopacity key={asset}> <image Source={asset.node.image} style={{width: (windowSize.width- -)/3, Height: the, margin:5}}/> </TouchableOpacity> ); } render () {return(<view Style={{flex:1}}> <camerarollview renderimage={ This._renderimage}/> </View> ) }}
React Native Study-cameraroll