2016-09-26 182 views
0

我有一个关于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它的工作很好,但是当我将每个图像保存在fileSystem(NSFileManager)中时我保存图像不好,没有解析和没有好质量请用任何方法解决这个问题以获取所有没有记忆警告的图像

非常感谢。

回答

0

您必须使用两个不同的apis来解决您的问题。 虽然渲染单元使用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) 
} 
+0

非常感谢你我的朋友它的工作,然后当我需要显示它的图像,我把它保存在文件系统我需要调整图像的大小是正确的? –

相关问题