使用CameraRoll.saveToCameraRoll将图像保存到相册?

3
我正在使用 RNCamera,它的效果非常好(以下是我的函数)。我可以拍照并将其保存到相册中。不过,有人能够指导如何保存到 iOS 中的特定相册吗?我已经看到了 Android 的解决方案,但该方法没有任何选项。不确定如何解决这个问题。谢谢提供帮助。 我认为我需要编辑 URI?这似乎不对,但只是在寻找想法。可能我需要修改 node_modules/react_native/Libraries/CameraRoll 下的库文件 'CameraRoll.js'?
-阅读 react-native 文档:https://facebook.github.io/react-native/docs/cameraroll.html -搜索了 stackoverflow(找到了 Android 的解决方案)/Google
async takePicture() {
if (this.camera) {
  const options = { quality: 0.5, based64: true }
  const data = await 
this.camera.takePictureAsync(options).then(data => 
    console.log(data.uri))
    CameraRoll.saveToCameraRoll(data.uri, 'photo'));
}

};


更新:我最终使用了 https://www.npmjs.com/package/react-native-store-photos-album 和 CameraRollExtended 组件。这使我能够轻松地将图片保存到我选择的相册中。 - yazmnh87
1个回答

1
你需要运行npm install react-native-fs ... 这是你的saveFile方法:
const saveFile = (base64) => {
  const albumPath = `${RNFS.PicturesDirectoryPath}/${APP_NAME}`;

  const fileName = `${new Date().getTime()}.png`;
  const filePathInCache = `${RNFS.CachesDirectoryPath}/${fileName}`;
  const filePathInAlbum = `${albumPath}/${fileName}`;

  return RNFS.mkdir(albumPath)
    .then(() => {
      RNFS.writeFile(filePathInCache, base64, 'base64').then(() => RNFS.copyFile(filePathInCache, filePathInAlbum)
      // Next step to show album without the need to re-boot your device:
        .then(() => RNFS.scanFile(filePathInAlbum))
        .then(() => {
          console.log('File Saved Successfully!');
        }));
    })
    .catch((error) => {
      console.log('Could not create dir', error);
    });
};

谢谢您,我一定会试一试并更新! - yazmnh87
PicturesDirectoryPath 未定义。它似乎只存在于 JS 端(原生代码中没有定义)。 - Cristiano Santos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接