问题
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