PhotoKit requestImage returns 低分辨率图像?

PhotoKit requestImage returns low res images?

当使用 Photo Kit 获取图像和视频时,我的图像和视频缩略图在我的 LazyVGrid 中以低分辨率显示。

当我在较大的图像视图中显示图像时,它们也会以低分辨率显示。

我所有的图片和视频都存储在 iCloud 上,所以我在 PHImageRequestOptions() 中设置了 isNetworkAccessAllowed = true AND deliveryMode = .highQualityFormat .

我尝试将 requestImage 方法中的 targetSize 更改为不同的值,但这也没有改变质量。

我做错了什么?

   @Published var fetchedMediaArray : [MediaAsset] = []

   // model for Assets
   struct MediaAsset: Identifiable, Hashable {
     var id = UUID()
     var image : UIImage
     var selected: Bool
     var asset: PHAsset
   }

   func requestAuth() {/*auth code*/ }

   func fetchPhotosAndVideos() { 
        let opt = PHFetchOptions()
        opt.includeHiddenAssets = false
        opt.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        opt.predicate = NSPredicate(format: "mediaType == %d || mediaType == %d",
                                             PHAssetMediaType.image.rawValue,
                                             PHAssetMediaType.video.rawValue)
        let req = PHAsset.fetchAssets(with: opt)
        
        DispatchQueue.main.async {
            let options = PHImageRequestOptions()
            options.isSynchronous = true
            options.deliveryMode = .highQualityFormat
            options.isNetworkAccessAllowed = true
            for j in 0..<req.count {
                
                PHCachingImageManager.default().requestImage(for: req[j], targetSize: CGSize(width: 100, height: 100), contentMode: .default, options: options) { (image, _) in
                    let data1 = MediaAsset(image: image!, selected: false, asset: req[j])
                    self.fetchedMediaArray.append(data1)
                   
                 }
             }
         }
     }

从云端下载照片时,它可能会多次调用您的完成处理程序。

来自 PHImageManager.requestImage(for:asset) 文档 -

Discussion

For an asynchronous request, Photos may call your result handler block more than once. Photos first calls the block to provide a low-quality image suitable for displaying temporarily while it prepares a high-quality image. (If low-quality image data is immediately available, the first call may occur before the method returns.) When the high-quality image is ready, Photos calls your result handler again to provide it. If the image manager has already cached the requested image at full quality, Photos calls your result handler only once. The PHImageResultIsDegradedKey key in the result handler’s info parameter indicates when Photos is providing a temporary low-quality image.

您可以在回调处理程序中做什么 -

  1. 如果您的资产以 target/requested 大小(或大于请求的大小)返回,您可以认为它已完成。
  2. 如果您的资产返回的尺寸小于 target/requested,您应该检查信息参数中的 PHImageResultIsDegradedKey 并等待照片下次调用您的完成。