0

I am trying to display a collection view of photo albums on the device. I am able to get the title but not sure how to get the Albums photo and set to the cell.

This is what I have:


import UIKit
import Photos

class PhotoAlbumViewController: UICollectionViewController {

    var albumList: PHFetchResult<PHAssetCollection>! = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        albumList = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: nil)
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        albumList.count
    }

    override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let album = albumList.object(at: indexPath.row)

        if let label = cell.viewWithTag(4000) as? UILabel {
            label.text = album.localizedTitle
        }
    }

}

I have a UI Label in my storyboard and label.text = album.localizedTitle sets the Album title correctly. Not sure how to get the image and set it to my Image component.

albertski
  • 2,428
  • 1
  • 25
  • 44
  • An album does not have an “image”. – matt Oct 16 '19 at 03:47
  • Would you like to take photos from the album? I see this answer for you https://stackoverflow.com/questions/32169185/how-to-fetch-all-images-from-custom-photo-album-swift/35178022 – Hiền Đỗ Oct 16 '19 at 03:51

1 Answers1

0

You can try this to get all ALBUM names from Photo Library

func get_All_Albums()
    {
        DispatchQueue.global(qos: .userInteractive).async
        {
            let fetchOptions = PHFetchOptions()
            fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)

            let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)
            let customAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)

            [smartAlbums, customAlbums].forEach {
                $0.enumerateObjects { collection, index, stop in

                    let requestOptions = PHImageRequestOptions()
                    requestOptions.isSynchronous = true
                    requestOptions.deliveryMode = .highQualityFormat

                    let photoInAlbum = PHAsset.fetchAssets(in: collection, options: fetchOptions)

                    if let title = collection.localizedTitle
                    {
                        if photoInAlbum.count > 0
                        {
                            print("\n\n \(title) --- count = \(photoInAlbum.count) \n\n")
                        }
                    }
                }
            }
        }
    }

& try this to get Photos from a specific Album

    func get_Photos_From_Album(albumName: String)
        {
            var photoLibraryImages = [UIImage]()
            var photoLibraryAssets = [PHAsset]()
            //whatever you need, you can use UIImage or PHAsset to photos in UICollectionView

            DispatchQueue.global(qos: .userInteractive).async
            {
                let fetchOptions = PHFetchOptions()
                fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)

                let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)
                let customAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)

                [smartAlbums, customAlbums].forEach {
                    $0.enumerateObjects { collection, index, stop in

                        let imgManager = PHImageManager.default()

                        let requestOptions = PHImageRequestOptions()
                        requestOptions.isSynchronous = true
                        requestOptions.deliveryMode = .highQualityFormat

                        let photoInAlbum = PHAsset.fetchAssets(in: collection, options: fetchOptions)

                        if let title = collection.localizedTitle
                        {
                            if photoInAlbum.count > 0
                            {
                                print("\n\n \(title) --- count = \(photoInAlbum.count) \n\n")
                            }

                            if title == albumName
                            {
                                if photoInAlbum.count > 0
                                {
                                    for i in (0..<photoInAlbum.count).reversed()
                                    {
                                        imgManager.requestImage(for: photoInAlbum.object(at: i) as PHAsset , targetSize: CGSize(width: 150, height: 150), contentMode: .aspectFit, options: requestOptions, resultHandler: {
                                            image, error in
                                            if image != nil
                                            {
                                                photoLibraryImages.append(image!)
 photoLibraryAssets.append(photoInAlbum.object(at: i))
                                            }
                                        })
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
pigeon_39
  • 1,503
  • 2
  • 22
  • 34