iOS PhotoKit:获取除全景图之外的所有智能相册 [英] iOS PhotoKit: Fetch all smart albums except panoramas

查看:249
本文介绍了iOS PhotoKit:获取除全景图之外的所有智能相册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

如何从此获取中排除Panoramas智能相册?我假设我必须使用options参数来添加谓词,但是我不知道如何格式化谓词.

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.

推荐答案

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

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
}

这篇关于iOS PhotoKit:获取除全景图之外的所有智能相册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆