how do i fetch an object?

I eventually figured it out. I realized i was not correctly converting the identifier to a UIImage so even if I had fixed the error in the OP it still would not work because of numerous others that would have come up with that solution. Below is the solution i got to work:

    var myCollectionView: UICollectionView!
    var imageViews = [UIImageView]()
    var identifier = [
    override func viewDidLoad() {
        let layout = UICollectionViewFlowLayout()
        myCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        myCollectionView.delegate = self
        myCollectionView.dataSource = self
        myCollectionView.backgroundColor = UIColor.white
        myCollectionView.allowsMultipleSelection = true
        myCollectionView.showsVerticalScrollIndicator = false
        myCollectionView.register(CollectionCell.self, forCellWithReuseIdentifier: "Cell")
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return identifier.count
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
        let options = PHFetchOptions()
        let results = PHAsset.fetchAssets(withLocalIdentifiers: identifier, options: options)
        let manager = PHImageManager.default()
        results.enumerateObjects { (thisAsset, _, _) in
            manager.requestImage(for: thisAsset, targetSize: CGSize(width: 200.0, height: 200.0), contentMode: .aspectFit, options: nil, resultHandler: {(thisImage, _) in
                self.imageViews.append(UIImageView(image: thisImage))
        let image = imageViews[indexPath.item]
        cell.imageView.image = image.image
        return cell

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top