iOS Group Photo Library photos by categories

1.1k Views Asked by At

In my app I am getting all photos(PHAsset) using this code

    PHPhotoLibrary.requestAuthorization { (status) in
        switch status {
        case .authorized:
            print("Good to proceed")
            let fetchOptions = PHFetchOptions()
            let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
            print("Found \(allPhotos.count) images")
        case .denied, .restricted:
            print("Not allowed")
        case .notDetermined:
            print("Not determined yet")
        @unknown default:
            fatalError()
        }
    }

I have two questions:

  1. Can I get grouped photos by categories from Photo Library? (see image below)
  2. Can I group photos by categories in my side? (see image below)

enter image description here

2

There are 2 best solutions below

2
NeZha On

It seems not can implement it directly.

But,you can classify by system smart album、moments or album(the result is PHAssetCollection type).

Swift 5

Smart Album

let systemAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: nil)

Moments

let systemAlbums = PHAssetCollection.fetchAssetCollections(with: .moment, subtype: .albumRegular, options: nil)

Album

let systemAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: nil)

and also can display the user's album(the fetch result is a PHCollection type that contained user collections)

let userAlbums = PHCollectionList.fetchTopLevelUserCollections(with: nil)

Besides: You needn't request authorization every time, the system will alert the user and request authorization at your first call every related function.

There is a sample code about browsing photos.Browsing and Modifying Photo Albums

0
holtmann On

There is no Public API to access the categories in 3rd party apps at the moment.