我正在尝试将远程照片(存储在Firebase存储中)保存到相机胶卷中,但是以下代码在Swift中未按预期运行:

PHPhotoLibrary.shared().performChanges({
    if let photoUrl = URL(string: "<see below for the url of the image>") {
        PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: photoUrl)
    }
}, completionHandler: { (success, error) -> Void in
    if let err = error {
        print(err)
    }
})

图片网址:

https://firebasestorage.googleapis.com/v0/b/nearbie-ee55a.appspot.com/o/wallpaper_pictures%2F52YgRFw4jWhYL5ulK11slBv7e583_E6A1D795-ED73-4C1E-98E1-CC97FF87E4C1?alt=media&token=0b090de3-43ab-426a-b63b-9d61372732d0

打印语句打印:
Error Domain=NSCocoaErrorDomain Code=-1 "(null)"

为什么会发生这种情况,我该如何解决?

编辑-这是正确的答案

首先下载图像,然后将其保存到相机胶卷中
URLSession.shared.dataTask(
    with: photoUrl,
    completionHandler: { (data, response, error) -> Void in
        if let imageData = data {
            PHPhotoLibrary.shared().performChanges({
                PHAssetChangeRequest.creationRequestForAsset(from: UIImage(data: imageData)!)
            }, completionHandler: { (success, error) -> Void in
                if let err = error {
                    print(err)
                }
            })
        }
    }
).resume()

最佳答案

您应该将图像下载到设备,然后将其保存到相机胶卷

10-08 15:44