List all photo albums in iOS

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)
    }
}

I use this to get just a few albums but you can get more:

private func setupPhotos() {
    let fetchOptions = PHFetchOptions()
    let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: fetchOptions)
    let topLevelUserCollections = PHCollectionList.fetchTopLevelUserCollections(with: fetchOptions)
    let allAlbums = [topLevelUserCollections, smartAlbums]

    allAlbums.enumerateObjects {(assetCollection, index, stop) in

        if #available(iOS 9.0, *) {
            fetchOptions.fetchLimit = 1
        }

        let assets = PHAsset.fetchAssets(in: assetCollection, options: fetchOptions)
        if let _ = assets.firstObject {
            let assetObject = MYSpecialAssetContainerStruct(asset: assets)
            self.myDataArray.append(assetObject)
        }
   }
   self.myDataArray.sortInPlace {(a, b) in
       return a.asset.localizedTitle < b.asset.localizedTitle
   }
   tableView.reloadData()
}

EDIT: This will get you the PHAssetCollections of the albums, then I put them in cells which have this method for getting the latest image thumbnail from the album.

private func downloadAndSetImage(asset: MYSpecialAssetContainerStruct) {
    guard asset.thumbnail == nil else {
        albumImage.image = asset.thumbnail
        return
    }

    let imageRequestOptions = PHImageRequestOptions()
    imageRequestOptions.isNetworkAccessAllowed = false
    imageRequestOptions.isSynchronous = true
    imageRequestOptions.deliveryMode = .highQualityFormat

    PHImageManager.default().requestImage(
        for: asset.asset,
        targetSize: CGSize(width: 200, height: 200),
        contentMode: .aspectFit,
        options: imageRequestOptions,
        resultHandler: {(img, info) in
            asset.thumbnail = img
            self.albumImage.image = asset.thumbnail
        }
    )
}

Tags:

Ios

Swift