iOS PhotoKit: Fetch all smart albums except panoramas

后端 未结 1 1179
清酒与你
清酒与你 2021-01-21 16:32

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

PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSub

相关标签:
1条回答
  • 2021-01-21 17:04

    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
    }
    
    0 讨论(0)
提交回复
热议问题