使用react-native init <projectName> 创建的项目,如果要使用到原生代码的部分,需要集成原生依赖项目;
如:CameraRoll 依赖的RCTCameraRoll;
步骤:
1、如果要在 iOS 上使用这个模块,我们首先要链接 RCTCameraRoll 库。进入到工程项目中的 node_module/react-native/Libraries/CameraRoll
image.png
2、把 RCTCameraRoll.xcodeproj 添加到在项目工程的 Liberaries 文件夹下
image.png
3、在 General -> Linked Frameworks and Libraries 里添加 libRCTCameraRoll.a
image.png
4、info.plist 中添加NSPhotoLibraryUsageDescription 和 NSPhotoLibraryAddUsageDescription
注意事项:iOS在模拟器上运行,可能无data值返回,请设置groupTypes为All,此值默认为SavedPhotos;
CameraRoll.getPhotos({
first : 20,
groupTypes: 'All',
assetType: 'Photos',
}).then(data => {
this.setState({photoSource : {uri : data.edges[3].node.image.uri}});
}).catch((error) => {
alert(error);
});
image.png