1

从 iOS 13 开始,我无法使用 PhotoKit 在“我的照片流”中获取原始分辨率版本的照片。在过去的几个月里,我收到了一些用户的投诉,所以这与我自己的设备或帐户无关。以下代码总是检索非常小的图像(例如 360 x 480)。

PHPhotoLibrary.requestAuthorization({ status in
    DispatchQueue.main.async {
        if status == .authorized {

            // if iCloud Photo Library is disabled and Photo Stream is enabled this will give you My Photo Stream
            let photoStream = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumMyPhotoStream, options: nil)
            if let photoStreamCollection = photoStream.firstObject {

                let res = PHAsset.fetchAssets(in: photoStreamCollection, options: nil)
                if let asset = res.firstObject {

                    let opts = PHImageRequestOptions()
                    opts.deliveryMode = .highQualityFormat
                    opts.isSynchronous = true
                    opts.isNetworkAccessAllowed = true
                    opts.resizeMode = .exact

                    print("asset size: \(asset.pixelWidth) x \(asset.pixelHeight)")

                    PHImageManager.default().requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .default, options: opts) { (img, info) in
                        if let img = img {
                            print("image size: \(img.size.width * img.scale) x \(img.size.height * img.scale)")
                        }
                    }

                    PHImageManager.default().requestImageDataAndOrientation(for: asset, options: opts) { (imgData, dateUTI, orientation, info) in
                        if let imgData = imgData {
                            if let img = UIImage(data: imgData) {
                                print("image size: \(img.size.width * img.scale) x \(img.size.height * img.scale)")
                            }
                        }
                    }
                }
            }
        }
    }
})

控制台:
资产大小:1536 x 2048
2020-02-02 08:18:44.452620-0700 photostreamphoto[7370:2576203] [ImageManager] [RM]: 1-1-0 无法解码图像
2020-02-02 08:18 :44.468675-0700 photostreamphoto[7370:2576203] [ImageManager] [RM]:1-1-0 无法解码图像
2020-02-02 08:18:44.479691-0700 photostreamphoto[7370:2576203] [ImageManager] [RM] : 1-1-0 无法解码图像
图像大小: 360.0 x 480.0
2020-02-02 08:18:44.485069-0700 photostreamphoto[7370:2576203] [ImageManager] [RM]: 1-2-0 无法加载图像来自 URL:file:///var/mobile/Media/PhotoStreamsData/267078714/100APPLE/IMG_0001.JPG
2020-02-02 08:18:44.490447-0700 photostreamphoto[7370:2576203] [ImageManager] [RM]: 1-2-0 无法从 URL 加载图像:file:///var/mobile/Media/PhotoStreamsData/ 267078714/100APPLE/IMG_0001.JPG
2020-02-02 08:18:44.501745-0700 photostreamphoto[7370:2576203] [ImageManager] [RM]: 1-2-0 无法从 URL 加载图像:file:///var /mobile/Media/PhotoStreamsData/267078714/100APPLE/IMG_0001.JPG
图像尺寸:360.0 x 480.0

我已经尝试了 PHRequestOptions 中所有可能的选项组合。请注意,如果我设置opts.version = .original它会给我一个零图像和零图像数据。

我还尝试asset.requestContentEditingInput(...)尝试直接访问图像 URL:

let editingOpts = PHContentEditingInputRequestOptions()
editingOpts.isNetworkAccessAllowed = true
editingOpts.canHandleAdjustmentData = { data in
    return true
}
asset.requestContentEditingInput(with: editingOpts) { input, info in
    if let input = input, let url = input.fullSizeImageURL {
        let path = url.path
        let fm = FileManager.default
        if fm.fileExists(atPath: path) {
            // doesn't reach this
        }
    }
}

安慰:

2020-02-02 08:26:49.641787-0700 photostreamphoto[7416:2578984] [PhotoKit] 资产资源文件 URL 无法访问,可能已被修剪,将尝试可用性更改请求

有趣的是,UIImagePicker 确实检索了原始分辨率图像(例如 1536 x 2048)。照片应用程序还显示全分辨率图像。我希望我知道他们在使用什么黑魔法。

如果有人知道解决方法,请分享。

4

0 回答 0