I have a
UICollectionView
to display photos from a device's album with the Photos
framework. The photos are correctly displayed, but if I scroll fast (like when you tape at the top of the screen to go to the top of the collectionView), I have some photos which are not at the good indexPath. I just need to scroll a bit to put the bad photo out of the screen, and everything go back in place.I clean the cell during
prepareForReuse
by canceling the current request.I presume it's a problem with the asynchronous request of
PHImageManager
, but I don't know how to avoid this problem. And I want to keet the asynchronous request to keep the collectionView smooth.Here some code :
View Controller
extension AlbumDetailViewController : UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photoList.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath) as! PhotoCollectionCell
cell.setImage(photoList.objectAtIndex(indexPath.row) as! PHAsset)
return cell
}
}
Custom CollectionViewCell
class PhotoCollectionCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
var requestId: PHImageRequestID!
let manager = PHImageManager.defaultManager()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
self.imageView.image = nil
manager.cancelImageRequest(self.requestId)
}
func setImage(asset: PHAsset) {
let option = PHImageRequestOptions()
option.resizeMode = .Fast
option.deliveryMode = .HighQualityFormat
self.requestId = manager.requestImageForAsset(asset, targetSize: CGSize(width: self.frame.size.width * UIScreen.mainScreen().scale, height: self.frame.size.height * UIScreen.mainScreen().scale), contentMode: PHImageContentMode.Default, options: option, resultHandler: {(result, info)->Void in
self.imageView.image = result
})
}
}
Thank you