Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all photo albums in iOS

Tags:

ios

swift

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?

like image 354
Ahmed M. Abed Avatar asked Apr 19 '16 08:04

Ahmed M. Abed


People also ask

How do I search for albums on my iPhone?

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.

How do I find all my photos on all my Apple devices?

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.

Can you alphabetize your albums on iPhone?

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.


1 Answers

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)
    }
}
like image 107
fatihyildizhan Avatar answered Oct 25 '22 22:10

fatihyildizhan