iOS PhotoKit:获取除全景照片以外的所有智能相册

时间:2016-11-22 21:35:18

标签: ios swift3 photokit

我使用以下代码来获取所有智能相册:

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

如何从此抓取中排除Panoramas智能相册?我假设我必须使用options param添加谓词,但我不知道如何格式化谓词。

1 个答案:

答案 0 :(得分:0)

如果要排除全景图,请考虑使用数组并仅获取所需的集合。换句话说,白名单集合。或者您可以枚举整个集合并排除全景图。白名单还可以控制集合的顺序。

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
}