2016-09-26 156 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

非常感謝你我的朋友它的工作,然後當我需要顯示它的圖像,我把它保存在文件系統我需要調整圖像的大小是正確的? –