【问题标题】:Get photos from specific album in swift using PHAssetCollection.fetchAssetCollectionsWithType()使用 PHAssetCollection.fetchAssetCollectionsWithType() 快速从特定相册中获取照片
【发布时间】:2015-11-12 20:55:30
【问题描述】:

当我启动照片获取时:

let assetCollections: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: nil) 

我得到了几张专辑的结果,所以我可以遍历数组并将它们的标题放在 tableView 中。

但是:当我使用谓词过滤并获取例如专辑“相机胶卷”时,结果始终是一个空数组:(而且我知道 100% 确定相机胶卷存在,因为它使用 nil 选项获取它)

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", "Camera Roll")
let assetCollections: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: fetchOptions)
let album: PHAssetCollection = assetCollections.firstObject as! PHAssetCollection

我已经在网上阅读了 4 或 5 种不同的方法,他们都有这个谓词,无论是“title = %@”、“localizedTitle = %@”还是“localizedIdentifier = %@”……我不知道'不明白。似乎为人们工作,而不是为我工作。编译器在试图“解开 nil 可选值”的最后一行崩溃(因为获取结果为空)。为什么只要包含 fetch 选项,搜索就会返回 nil ???

【问题讨论】:

    标签: swift filter fetch predicate photos


    【解决方案1】:

    如果你想要相机胶卷专辑,你最好象征性地要求它:

    let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)
    

    SmartAlbumUserLibrary 子类型是获取相机胶卷的方式(并不总是这样称呼)。

    【讨论】:

      【解决方案2】:

      解决方案似乎是使用:

      PHAssetCollection.fetchAssetCollectionsWithLocalizedIdentifiesr(identifiers: [String], options: PHFetchOptions)
      

      因此,将带有我们希望获取的专辑标题的字符串数组传递给参数“标识符”,与尝试似乎不再起作用的谓词方法获得相同的结果。

      【讨论】:

      • 虽然这段代码可以回答这个问题,但最好在不介绍其他代码的情况下解释它是如何解决问题的,以及为什么要使用它。从长远来看,纯代码的答案没有用处。
      【解决方案3】:

      如果你只想要相机胶卷专辑,你可以试试这样的:

      func getCameraRoll() -> AlbumModel {
            var cameraRollAlbum : AlbumModel!
      
            let cameraRoll = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil)
      
            cameraRoll.enumerateObjects({ (object: AnyObject!, count: Int, stop: UnsafeMutablePointer) in
              if object is PHAssetCollection {
                let obj:PHAssetCollection = object as! PHAssetCollection
      
                let fetchOptions = PHFetchOptions()
                fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
                fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
                let assets = PHAsset.fetchAssets(in: obj, options: fetchOptions)
      
                if assets.count > 0 {
                  let newAlbum = AlbumModel(name: obj.localizedTitle!, count: assets.count, collection:obj, assets: assets)
      
                  cameraRollAlbum = newAlbum
                }
              }
            })
           return cameraRollAlbum
      
          }
      

      请注意,您应该 AlbumModel 是我创建的模型,您可以更改它并使用自己的数据模型。

      【讨论】:

      • 嗨,为什么第一次调试器没有进入 cameraRoll.enumerateObjects。
      【解决方案4】:
      let fetchOptions = PHFetchOptions()
      fetchOptions.predicate = NSPredicate(format: "localizedTitle = %@", "Camera Roll")
      let assetCollections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
      let album = assetCollections.firstObject
      

      【讨论】:

        【解决方案5】:

        试试格式:

        fetchOptions.predicate = NSPredicate(format: "%K == %@", "title", "Camera Roll")

        fetchOptions.predicate = NSPredicate(format: "%K LIKE[cd] %@", "title", "Camera Roll")

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-08-22
          • 1970-01-01
          • 1970-01-01
          • 2013-05-28
          • 1970-01-01
          • 2015-02-10
          • 2012-07-03
          • 1970-01-01
          相关资源
          最近更新 更多