简体   繁体   English

PhotoKit requestImage 返回低分辨率图像?

[英]PhotoKit requestImage returns low res images?

When fetching images and video with Photo Kit my images and video thumbnails are displaying in low res within my LazyVGrid.当使用 Photo Kit 获取图像和视频时,我的图像和视频缩略图在我的 LazyVGrid 中以低分辨率显示。

They also display in lo-res when I display the image inside a larger Image view.当我在较大的图像视图中显示图像时,它们也会以低分辨率显示。

All of my images and video are stored on iCloud so I set isNetworkAccessAllowed = true AND deliveryMode = .highQualityFormat in PHImageRequestOptions().我所有的图像和视频都存储在 iCloud 上,所以我在 PHImageRequestOptions() 中设置了isNetworkAccessAllowed = true AND deliveryMode = .highQualityFormat

I have tried changing the targetSize in the requestImage method to different values but that didn't change the quality either.我尝试将 requestImage 方法中的 targetSize 更改为不同的值,但这也没有改变质量。

What am I doing wrong?我究竟做错了什么?

   @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)
                   
                 }
             }
         }
     }

When Photos are being downloaded from cloud, it may call your completion handler multiple times.当从云端下载照片时,它可能会多次调用您的完成处理程序。

From the PHImageManager.requestImage(for:asset) docs -来自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.结果处理程序的信息参数中的PHImageResultIsDegradedKey键指示照片何时提供临时低质量图像。

What you can do in your callback handler -您可以在回调处理程序中做什么 -

  1. If your asset is being returned in target/requested size(or bigger than requested), you can consider it done.如果您的资产以目标/请求的大小(或大于请求的大小)返回,您可以认为它已完成。
  2. If your asset is being returned in smaller than target/requested size, you should check for PHImageResultIsDegradedKey in the info parameter and wait for Photos to call your completion next time.如果您的资产以小于目标/请求的大小返回,您应该检查信息参数中的PHImageResultIsDegradedKey并等待照片下次调用您的完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM