0

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()

    }
}
Tanjima Kothiya
  • 5,695
  • 2
  • 8
  • 17
  • 1
    Where is the code, I can't see it. – iDeveloper Sep 27 '21 at 12:12
  • only load thumbnails to memory and lazy load them, scroll to end to load more pages – Quang Hà Sep 27 '21 at 12:18
  • What are you trying to do with the photos? Would it not be easier to show a PHPickerViewController and then just deal with the handful of photos the user wants to use in your app. – Fogmeister Sep 27 '21 at 12:35
  • Why do you need to load the table guy with all 7,000 images at a time? Is your table guy capable of showing all 7,000 rows of images at a time? What is the size of each cell? – El Tomato Sep 28 '21 at 07:45

0 Answers0