iosswiftphotos

How to fetch Photos album title & count in swift?


No matter what I look for, there is no answer to my question.

I am building the iOS standard photos app.

Imagine when you turned on the Photos app.

The album title and the number of images in the album are displayed. (Like the picture)enter image description here

If there is no picture such as a panorama or selfie, the album title does not appear. (Excluding recently deleted)

I want to know the album title and the number of pictures that I can know when I run the photo app like this. How do I access it?


Solution

  • You can use Photos framework with fetchAssetCollections

    Don't forget to adda Privacy - Photo Library Usage Description Key in your info.plist

    import Photos
    
    class ViewController: UIViewController {
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let albumsPhoto:PHFetchResult<PHAssetCollection> = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: nil)
    
            albumsPhoto.enumerateObjects({(collection, index, object) in
                let photoInAlbum = PHAsset.fetchAssets(in: collection, options: nil)
                    print(photoInAlbum.count)
                    print(collection.localizedTitle)
    
            })
        }
    }