无法在Swift

时间:2018-05-09 22:15:45

标签: ios swift

我正在尝试将远程照片(存储在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

print语句打印:

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()

1 个答案:

答案 0 :(得分:1)

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