Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何从iCloud下载图像而不重新保存?_Swift_Photokit_Phimagemanager - Fatal编程技术网

Swift 如何从iCloud下载图像而不重新保存?

Swift 如何从iCloud下载图像而不重新保存?,swift,photokit,phimagemanager,Swift,Photokit,Phimagemanager,我在尝试从图库中挑选照片时遇到问题(当我挑选上传到iCloud的图像时,应用程序崩溃)。 所以我的第一个解决方案是检查设备上是否有图像,如果没有,然后从iCloud下载 我用了这个密码 let manager = PHImageManager.default() let requestOptions = PHImageRequestOptions() requestOptions.resizeMode = .exact requestOptions.deliveryMode = .highQua

我在尝试从图库中挑选照片时遇到问题(当我挑选上传到iCloud的图像时,应用程序崩溃)。 所以我的第一个解决方案是检查设备上是否有图像,如果没有,然后从iCloud下载

我用了这个密码

let manager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.resizeMode = .exact
requestOptions.deliveryMode = .highQualityFormat;

// Request Image
manager.requestImageData(for: asset, options: requestOptions, resultHandler: { (data, str, orientation, info) -> Void in
// Do somethign with Image Data
})
在这种情况下,我会获取图像数据,但如果我想保存它,它会保存到新照片中。所以我得到的是照片中的图像副本,而不是存储在设备上的原始图像

所以我的问题是如何下载图像而不重新保存它。
基本上,我想要与照片应用程序中相同的行为(当照片不在设备上时,它会下载照片并将其保存到同一张图片中,而不是创建照片的副本)

我遇到了这个问题,这里有一个解决方法:

设置为
false

requestOptions.isNetworkAccessAllowed = false
那是因为你需要确保我们不会从iCloud获取它。此时,如果未下载图像,则
resultHandler
中的
数据应为
nil

manager.requestImageData(for: PHAsset(), options: requestOptions, resultHandler: { (data, str, orientation, info) -> Void in
    if let fetchedData = data {
        // the image is already downloaded
    } else {
        // the image is not downloaded...
        // now what you should do is to set isNetworkAccessAllowed to true
        // and make a new request to get it from iCloud.
    }
})
此外,您还可以利用:

根据文件中所述,请记住:

如果为true,则不提供图像,因为资产数据必须为 从iCloud下载。要下载数据,请提交另一个请求, 并为isNetworkAccessAllowed选项指定true


所以,若我设置允许网络访问,那个么照片将下载到原始图像而不重新保存?
manager.requestImageData(for: PHAsset(), options: requestOptions, resultHandler: { (data, str, orientation, info) -> Void in
    if let keyNSNumber = info?[PHImageResultIsInCloudKey] as? NSNumber {
        if keyNSNumber.boolValue {
            // its on iCloud...
            // read the "keep in mind" below note,
            // now what you should do is to set isNetworkAccessAllowed to true
            // and make a new request.
        } else {
            //
        }
    }
})