I'm struggling with the problem of displaying photo gallery from iPhone to collectionView
.
Everything works fine if someone has 50 photos inside the gallery. The problem is when someone has thousands of photos, then the gallery is loading for 10 seconds, which is not good for my app.
The same problem occurs when I'm loading images from Facebook. The app is waits until it downloads every photo then it displays. I'd like to display images one by one during the loading operation instead of waiting for it to load it all.
I know that I should use DispachQueue
and I did but there is some bug that I don't see.
Here is the code I use to fetch images from iPhone gallery:
func grapPhotos() {
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if let fetchResulat : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {
if fetchResulat.count > 0 {
for i in 0..<fetchResulat.count {
imgManager.requestImage(for: fetchResulat.object(at: i), targetSize: CGSize(width: 200, height:200), contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: {
(image, eror) in
self.imageArray.append(image!)
DispatchQueue.main.async {
self.collectionView.reloadData()
}
})
}
} else {
print("You have no photos")
self.collectionView.reloadData()
}
}
}
And the code to display them in collectionView
:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "galleryCell", for: indexPath) as! GalleryCollectionViewCell
let image = self.imageArray[indexPath.row]
DispatchQueue.main.async {
cell.gelleryImages.image = image
}
return cell
}
Probably the same problem is with my Facebook class so I will be very grateful for your help.