I am trying following method to fetch asset from local identifier and displaying it in collection view but scroll become jerky while loading.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GridCell", for: indexPath) as! GridCell
cell.representedAssetIdentifier = "some uri"
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = false
let asset = PHAsset.fetchAssets(withLocalIdentifiers: ["some uri"!], options: .none).firstObject
imageManager.requestImage(for: asset!, targetSize: thumbnailSize, contentMode: .aspectFit, options: requestOptions, resultHandler: { result, info in
if cell.representedAssetIdentifier =="someuri" {
cell.imageview.image = result
}
})
expensive operations should not be performed in the
cellForItemAt
method. The choppyness is because scrolling is delayed till that method has completed. This is specifically cause by the line:remove that line and you will see that scrolling will be as smooth as butter. To fix this you need to be smarter about where and when that line gets executed if its reusable then do it once on
viewDidLoad
and store it as a variable.