iOS PhotoKit: Fetch all smart albums except panoramas

谁说我不能喝 提交于 2019-12-20 04:23:09

问题


I am using the follow code to fetch all smart albums:

PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.albumRegular, options: nil)

How can I exclude the Panoramas smart album from this fetch? I assume I have to add a predicate using the options param, but I don't know how to format the predicate.


回答1:


If you want to exclude the Panoramas, consider using an array and fetching only the collection you need. In other words, whitelisting collections. Or you can enumerate through the collections and exclude the Panoramas. Whitelisting also gives you control of the order of collections.

var smartAlbums: [PHAssetCollection] = []
let subtypes:[PHAssetCollectionSubtype] = [
    // all photos collection
    // .smartAlbumUserLibrary,
   .smartAlbumFavorites,
   .smartAlbumPanoramas,
   .smartAlbumLivePhotos,
   .smartAlbumBursts,
   .smartAlbumDepthEffect,
   .smartAlbumLongExposures,
   .smartAlbumScreenshots,
   .smartAlbumSelfPortraits
]

smartAlbums = fetchSmartCollections(with: .smartAlbum, subtypes: subtypes)

private func fetchSmartCollections(with: PHAssetCollectionType, subtypes: [PHAssetCollectionSubtype]) -> [PHAssetCollection] {
    var collections:[PHAssetCollection] = []
    let options = PHFetchOptions()
    options.includeHiddenAssets = false

    for subtype in subtypes {
        if let collection = PHAssetCollection.fetchAssetCollections(with: with, subtype: subtype, options: options).firstObject {
            collections.append(collection)
        }
    }

    return collections
}


来源:https://stackoverflow.com/questions/40752069/ios-photokit-fetch-all-smart-albums-except-panoramas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!