【问题标题】:How to download image from iCloud without re-saving it?如何在不重新保存的情况下从 iCloud 下载图像?
【发布时间】:2020-01-13 19:27:22
【问题描述】:

我在尝试从图库中挑选照片时遇到问题(当我挑选上传到 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
})

我在这种情况下获得了 imageData,但如果我想保存它,它会保存到 新照片。因此,我在照片中获得了图像的副本,而不是获得一个存储在设备上的原始图像

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

【问题讨论】:

    标签: swift photokit phimagemanager


    【解决方案1】:

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

    isNetworkAccessAllowed 设置为false

    requestOptions.isNetworkAccessAllowed = false
    

    那是因为您需要确保我们不会从 iCloud 中获取它。此时如果没有下载图片,resultHandler中的data应该是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.
        }
    })
    

    此外,您可以利用PHImageResultIsInCloudKey

    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 {
                //
            }
        }
    })
    

    PHImageResultIsInCloudKey 文档中所述,请记住:

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

    【讨论】:

    • 那么如果我设置了允许网络访问,那么照片将被下载到原始图像而不重新保存?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多