Post

Replies

Boosts

Views

Created

Populating UICollectionView with Images from Camera Roll
Hi, I'm trying to recreate an image gallery like the one on the "Photos" App of the iPhone. This is the code I'm using (Swift 5): import UIKit import Photos private let reuseIdentifier = "dataCell" class GalleryCollectionViewController: UICollectionViewController {     var images = [PHAsset]()     override func viewDidLoad() {         super.viewDidLoad()         self.collectionView!.register(GalleryImageCell.self, forCellWithReuseIdentifier: reuseIdentifier)         getImages()     }          func getImages() {         let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)         assets.enumerateObjects({ (object, count, stop) in             self.images.append(object)         })                  self.images.reverse()         self.collectionView!.reloadData()     }          override func numberOfSections(in collectionView: UICollectionView) - Int {         return 1     }               override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) - Int {         return images.count     }          override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) - UICollectionViewCell {         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dataCell", for: indexPath) as! GalleryImageCell                  let asset = images[indexPath.row]         let ImageManager = PHImageManager.default()         if cell.tag != 0 {             ImageManager.cancelImageRequest(PHImageRequestID(cell.tag))         }                  cell.tag = Int(ImageManager.requestImage(for: asset, targetSize: CGSize(width: 120, height: 120), contentMode: .aspectFill, options: nil, resultHandler: { (result, _) in             cell.galleryImage?.image = result         }))                  return cell     }          func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) - CGSize {         let width = self.view.frame.width * 0.32         let height = self.view.frame.height * 0.179910045         return CGSize(width: width, height: height)     }          func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) - CGFloat {         return 2.5     }          func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) - UIEdgeInsets {         return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)     }          func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) - CGFloat {         return 0     }      } Anyway, when I load the view I only get a black view. The images are not displayed at all even though the cells seem to be created as the view scrolls for a good amount of time. It is not a problem related to Privacy Permissions as I've already granted access to "All Photos" and because ''images'' array contains all the photos assets. Do you know what I might be doing wrong? Thank you in advance, Alberto
6
0
1.3k
Mar ’21