使用react-native init <projectName> 創(chuàng)建的項(xiàng)目,如果要使用到原生代碼的部分硕盹,需要集成原生依賴項(xiàng)目符匾;
如:CameraRoll 依賴的RCTCameraRoll;
步驟:
1、如果要在 iOS 上使用這個(gè)模塊莱睁,我們首先要鏈接 RCTCameraRoll 庫待讳。進(jìn)入到工程項(xiàng)目中的 node_module/react-native/Libraries/CameraRoll
image.png
2、把 RCTCameraRoll.xcodeproj 添加到在項(xiàng)目工程的 Liberaries 文件夾下
image.png
3仰剿、在 General -> Linked Frameworks and Libraries 里添加 libRCTCameraRoll.a
image.png
4、info.plist 中添加NSPhotoLibraryUsageDescription 和 NSPhotoLibraryAddUsageDescription
注意事項(xiàng):iOS在模擬器上運(yùn)行痴晦,可能無data值返回南吮,請?jiān)O(shè)置groupTypes為All,此值默認(rèn)為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