I am using UICollectionView for an album. I got the picture to load on iPhone 12. I got it to load about 100 pictures but if there are over 3000 pictures when I scroll the UICollectionView down it is very slow and sometimes the app stops.
I am using this code to get the pictures.
func grabPhotos(){
imageArray = []
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let assetsFetchResult = (collection == nil) ? PHAsset.fetchAssets(with: .image, options: options) : PHAsset.fetchAssets(in: collection!, options: nil)
assets = assetsFetchResult.objects(at: IndexSet(integersIn: Range(NSMakeRange(0, assetsFetchResult.count))!))
let imgManager=PHImageManager.default()
let requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
requestOptions.deliveryMode = .highQualityFormat
requestOptions.resizeMode = .exact
requestOptions.version = .original
requestOptions.isNetworkAccessAllowed = true
if assetsFetchResult.count > 0 {
for i in 0..<assetsFetchResult.count{
imgManager.requestImage(for: assetsFetchResult.object(at: i) as PHAsset, targetSize: CGSize(width:150, height: 200) ,contentMode: .aspectFill, options: requestOptions, resultHandler: { (image, error) in
if image != nil {
DispatchQueue.main.async(execute: {
self.imageArray.append(image!)
})
}
})
}
} else {
print("You got no photos.")
}
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
self.categoryView.reloadData()
self.activityIndicator.stopAnimating()
}
}