反应本机 (ios)。 "saveToCameraRoll" 错误 - "PHPhotosErrorDomain error -1"

React native (ios). "saveToCameraRoll" error - "PHPhotosErrorDomain error -1"

当我尝试将图像保存到 phone(模拟器 IPhone 11 - iOS 13.1)图库时:

import CameraRoll from "@react-native-community/cameraroll";

...

CameraRoll.saveToCameraRoll('https://example.com/images/1.jpg', 'photo')
.then(() => {
  alert('Saved to photos');
})
.catch((err) => {
  console.log('err:', err);
});

,它给我一个错误:

err: [Error: The operation couldn’t be completed. (PHPhotosErrorDomain error -1.)]

我将这些行添加到文件 "Info.plist":

<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app requires access to the photo library</string>

和 "ios" 文件夹中的 运行 命令:

pod update
pod install

但是错误依旧。我需要做些什么来清除错误吗?

我刚遇到同样的情况,以下是我的解决方法:

我已经使用 rn-fetch-blob:

下载了文件
let file
RNFetchBlob
    .config({
        fileCache: true,
        appendExt: path.substring(path.lastIndexOf('.')+1, path.length)
           //get the file's extension from the URL it's coming from and append to the file name that RNFectchBlob will create locally
    })
    .fetch('GET', path) //GET request to URL which has your file
    .then((res) => {
        file = res //var 'file' is the temp object which contains all data from our file
    })

在您的应用程序本地保存文件后,我们现在可以调用 CameraRoll 来保存文件:

CameraRoll.saveToCameraRoll(file.path())
.then(Alert.alert("Done!", "Your file has been saved."))

如果这对某人有帮助,

我在 React Native Cameraroll 4.1.2 版中遇到了同样的错误。 (iOS)

"react-native": "0.63.3".

我通过在图像路径中为saveToCameraRoll/save方法添加前缀“file://”解决了这个问题。