【问题标题】:Swift Fetching smartAlbum with at least one media使用至少一种媒体快速获取 smartAlbum
【发布时间】:2017-10-10 11:23:46
【问题描述】:

我正在获取用户库中存在的所有 smartAlbum。 我使用的代码是这样的:

 var smartAlbums: PHFetchResult<PHAssetCollection>!

override func viewDidLoad() {
    super.viewDidLoad()
let options = PHFetchOptions()
    options.predicate = NSPredicate(format: "estimatedAssetCount > 0")
    options.sortDescriptors = [NSSortDescriptor(key: "localizedTitle", ascending: true)]
    smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: options)

我将在表格视图中显示结果,并使用它来计算部分中的行数:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch Section(rawValue: section)! {
    case .allPhotos: return 1
    case .smartAlbums: return smartAlbums.count
    case .userCollections: return userCollections.count
    }
}

一切正常...但 smartAlbums 的提取结果也提取了零媒体的专辑。基本上它会获取所有专辑。似乎谓词它没有被考虑在内。 相同的谓词应用于 userCollections 并且效果很好。

 userCollections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: options) // this works fine

有没有办法只获取包含至少一种媒体的 smartAlbums?

谢谢!

【问题讨论】:

    标签: ios swift fetch photosframework


    【解决方案1】:

    我就是这样解决的:

    我创建了一个扩展:

    extension PHAssetCollection {
    var photosCount: Int {
        let fetchOptions = PHFetchOptions()
        fetchOptions.predicate = NSPredicate(format: "mediaType == %d OR mediaType == %d", PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)
        let result = PHAsset.fetchAssets(in: self, options: fetchOptions)
        return result.count
    }
    

    }

    然后在我要获取对象的 tableview 控制器中,我创建了一个子类型数组(这不是为了获取所有 smartAlbums,而是只获取我需要的一个),并且我已经初始化了一个 PHCollection 类型的数组:

      let subtypes:[PHAssetCollectionSubtype] = [
        .smartAlbumFavorites,
        .smartAlbumPanoramas,
        .smartAlbumScreenshots,
        .smartAlbumSelfPortraits,
        .smartAlbumVideos,
        .smartAlbumRecentlyAdded,
        .smartAlbumSelfPortraits
    ]
    var smartAlbums: [PHAssetCollection] = []
    

    然后我创建了一个函数来获取与子类型相关联的所有 smartAlbums,并且其中至少包含一个媒体(图像或视频 -> 这是使用先前创建的扩展程序执行的):

    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, collection.photosCount > 0 { // .photosCount comes from extesion
                collections.append(collection)
            }
        }
    

    并在 vieDidLoad 中获取对象:

    override func viewDidLoad() {
        super.viewDidLoad()
    smartAlbums = fetchSmartCollections(with: .smartAlbum, subtypes: subtypes)
    }
    

    我是 swift 的新手,所以我不知道这是否是最好的解决方案,但它有效,并且看到我在网上没有找到任何可以帮助我的东西,如果有人需要,我想分享一下.

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 2016-04-21
      • 2021-07-05
      • 2023-04-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多