I need to get all photo albums on iPhone.
I know I can start an image picker to let the user selects an image from it. But I need to develop my own image browser.
All the code I found work with image picker or they already know album name.
I need first to list all photo albums names then the user will select one of them, then I will display images in this album.
All these steps will be done in customized layouts.
Can I list all photo albums?
Albums. In the Albums tab, you can find the albums you've created and the shared albums you've created or joined, as well as collections of different types of photo and video, such as Selfies, Portrait and Slo-mo.
Choose iCloud, then turn on iCloud Photos. Note: You can't select both iCloud Photos and My Photo Stream on Apple TV. To view photos and videos stored in iCloud Photos, open the Photos app, then navigate to the Photos category in the menu bar. All the photos and videos you see are stored in iCloud Photos.
You can sort the photos inside the albums automatically, if you have iOS 14 or later installed. But the only way to arrange the albums in an alphabetical order is to tap Edit, then drag the albums into the order you want.
Simple way to list all albums with image counts.
class AlbumModel {
let name:String
let count:Int
let collection:PHAssetCollection
init(name:String, count:Int, collection:PHAssetCollection) {
self.name = name
self.count = count
self.collection = collection
}
}
func listAlbums() {
var album:[AlbumModel] = [AlbumModel]()
let options = PHFetchOptions()
let userAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: options)
userAlbums.enumerateObjects{ (object: AnyObject!, count: Int, stop: UnsafeMutablePointer) in
if object is PHAssetCollection {
let obj:PHAssetCollection = object as! PHAssetCollection
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
let newAlbum = AlbumModel(name: obj.localizedTitle!, count: obj.estimatedAssetCount, collection:obj)
album.append(newAlbum)
}
}
for item in album {
print(item)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With