使用photos框架从照片库中获取所有图像



我有一个关于PHCachingImageManager的问题,我如何在我的自定义集合视图中处理数千张图像?

我在viewWillAppear中的代码如下:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    for i in 0..<phasset.count - 1 {
        let getAsset = phasset[i]
        catchingImage.startCachingImages(for: [getAsset], targetSize: CGSize(width:125,height:125), contentMode: .aspectFit, options: nil)
    }
}

和我的代码在cellForIrem在集合视图如下。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? thumbNailCell
    catchingImage.requestImage(for: phasset[indexPath.row], targetSize: CGSize(width:125,height:125), contentMode: .aspectFill, options: nil) { (img, nil) in
        cell?.ImageView.image = img!
    }
    return cell!
}

但是当我从照片库加载所有图像时,我也得到了内存问题。

当我将图像大小调整为100.0 X 100.0时,它的工作很好,但是当我将每个图像保存在文件系统(NSFileManager)中时,我保存图像不好,没有分辨率和质量不好,请用任何方法来解决这个问题,以便在没有内存警告的情况下获取所有图像

您必须使用两个不同的api来解决您的问题。当渲染到单元格时,使用上面提到的catchinImage api

catchingImage.requestImage(for: phasset[indexPath.row], targetSize: CGSize(width:100,height:100), contentMode: .aspectFill, options: nil) { (img, nil) in
    cell?.ImageView.image = img!
}

但是当涉及到将图像保存到文件系统时,你应该使用PHImageManager而不是catchingmanager

let options = PHImageRequestOptions()
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat
options.networkAccessAllowed = YES //download from iCloud if required
PHImageManager.defaultManager().requestImageDataForAsset(phasset[indexPath.row], options: options) { (data, _, _, _) in
    let retrievedData = NSData(data: retrievedCFData)
    retrievedData.write(toFile:filepath atomically:NO)
}

最新更新