use*_*328 5 caching image ios swift
目标
操纵几个高分辨率的本地图像而不捕获它们,然后在集合视图中选择并显示其中一些图像.
限制
PHImageManager没有选项来检索本地图像而不捕获它.PHAsset没有指定其本地路径的属性.最后,我不可能使用UIImage或NSData对象的数组,因为它快速达到内存限制并崩溃.
工具
Xcode 8.1 - Swift 3.0.1
现在的情况
首先,我获取资产,然后遍历它们以获取它们的本地路径.获取本地路径的原因是访问它们而不使用UIImage(contentsOfFile: localPath)或捕获它们UIImage.init(data: Data(contentsOf: localPath)).然后,我在图像上运行一些业务逻辑,然后选择其中一些.最后,我将所选图像的本地路径存储在一个数组中,然后由集合视图使用.
注意:在iOS 10或更高版本中,requestContentEditingInput 完成处理程序是在主队列上调用的.
问题
一切都有效,直到集合视图尝试加载本地图像.使用完成处理程序UIImage(contentsOfFile: localPath)内部成功加载图像的本地路径requestContentEditingInput.但是,在collectionView(_ collectionView:, cellForItemAt indexPath:)它带来的集合视图函数内使用相同的函数加载时,同一图像的本地路径nil.
因此,我想知道我错过了什么或做错了什么.我已经尝试使用资产cancelContentEditingInputRequest思维,可能是图像上有一种锁定,但它没有解决问题.
代码示例
var imagesSelected = [String]()
@IBOutlet weak var imageCollectionView: UICollectionView!
private func processImages() {
  ...
  assets.enumerateObjects({ (asset, count, stop) in 
    ...
    asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions(), completionHandler: { (input, _) in
      if let url = input?.fullSizeImageURL {
        if let image = UIImage(contentsOfFile: url.relativePath) {
          ...
          if (...) {
            self. imagesSelected.append(url.relativePath)
            self.imageCollectionView.reloadData()
          }
        }
      }
    })
  })
}
// MARK: - UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath as IndexPath) as! ImageCollectionViewCell
  cell.productImage.image = UIImage(contentsOfFile: self.images[indexPath.row])
  return cell;
}
| 归档时间: | 
 | 
| 查看次数: | 363 次 | 
| 最近记录: |