我正在使用 iOS 中的照片框架为我的应用程序开发一个叠加层,其工作方式类似于 WhatsApp 叠加层。为了访问用户的图库图像 Assets ,我需要使用照片,并且不能逃避使用 UIImagePickerController。
为了更好地介绍该框架,我决定首先构建自己的图像选择器,并开始从我的画廊加载图像并将其放入简单屏幕上的 Collection View 中。一旦我使用 PHAsset.fetchAssets(with: .image, options: fetchOptions) as PHFetchResult<PHAsset>
获得 Assets
然后,我在一个数组中使用这些 Assets ,并在默认的 PHCachingManager 中开始缓存,如下所示:
cachingImageManager.startCachingImages(for: self.assets,
targetSize: CGSize(width: 150.0, height: 150.0),
contentMode: .default,
options: nil)
在我的 collectionView 的 cellForItemAtIndexPath 中,我使用:
cachingImageManager.requestImage(for: assets[indexPath.row], targetSize: CGSize(width: 150.0, height: 150.0), contentMode: .default, options: nil, resultHandler: { (image, info) in
cell.imageView.image = image
})
如您所见,根据 Apple 文档,我在两个地方都使用了具有完全相同的 targetSize、contentMode 和选项的缓存,因此,我设置图像的回调 block 应该以高质量调用一次图片。
但是,如果我重新加载 collectionView,甚至在第一次加载 Collection View 时,回调会被调用两次,一次是低质量图像,第二次是高质量图像。
我忍不住认为这是我下载的错误 Apple's tutorial code 对于 Photos 框架,即使文档明确指出如果我们为 Assets 发送相同的选项、contentMode 和 targetSize,它也会在缓存时仅调用一次。 (无论您重新加载集合多少次,即使是教程代码也会先加载低质量图像,然后再加载高质量图像)。
任何帮助将不胜感激,因为我是 AVFoundation 编码的新手并且被这个问题难住了。
如果这篇文章真的很长,我很抱歉,但在此先感谢您的帮助!
最佳答案
了解 PHImageRequestOptions
和属性 deliveryMode
可能默认值设置为 opportunistic
case opportunistic
Photos automatically provides one or more results in order to balance image quality and responsiveness.
关于ios - PHCachingImageManager 不缓存 Swift 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39759025/