– Use photos framework instead of ALAsset library.
– You will get all albums in smartAlbums & userCollections.
– If you want the albums which contain at least 1 photos refer below code.
– This code will work on swift.
var arrAlbums: [PHCollection]! = Array()
var smartAlbums: PHFetchResult!
var userCollections: PHFetchResult!
smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)
userCollections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
func AlbumContainsPhotos() {
for i in 0 ..< smartAlbums.count {
var collection: PHCollection
collection = smartAlbums.object(at: i)
guard let assetCollection = collection as? PHAssetCollection
else { fatalError("expected asset collection") }
var fetchResult: PHFetchResult!
fetchResult = PHAsset.fetchAssets(in: assetCollection, options: fetchOptions)
if fetchResult.count > 0 {
arrAlbums.append(collection)
}
}
for i in 0 ..< userCollections.count {
var collection: PHCollection
collection = userCollections.object(at: i)
guard let assetCollection = collection as? PHAssetCollection
else { fatalError("expected asset collection") }
var fetchResult: PHFetchResult!
fetchResult = PHAsset.fetchAssets(in: assetCollection, options: fetchOptions)
if fetchResult.count > 0 {
arrAlbums.append(collection)
}
}
}