【问题标题】:estimatedAssetCount returns a wrong countestimateAssetCount 返回错误的计数
【发布时间】:2016-06-01 23:23:00
【问题描述】:

我需要显示包含图像数量的相机胶卷相册。我正在使用下面的代码来获取相册。

let smartCollections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)
smartCollections.enumerateObjectsUsingBlock { object, index, stop in
    if let collection = object as? PHAssetCollection {
        print(collection.estimatedAssetCount)
    }
}

“照片”应用的相机胶卷中只有 28 张图片。但是estimatedAssetCount 属性返回值 9223372036854775807!

这只发生在操作系统创建的相册(如相机胶卷)上。对于用户创建的常规相册,返回正确的值。我做错了什么还是这是一个错误?

如果是,有没有其他方法可以获得正确的图像计数?

【问题讨论】:

    标签: ios swift count photokit phassetcollection


    【解决方案1】:

    应该看起来更长一点。进入PHAssetCollection 的头文件会发现这个小信息。

    这些数字只是估计值;返回的对象的实际数量 如果您关心准确性,则应使用 from a fetch 。退货 如果无法快速返回计数,则为 NSNotFound。

    所以我猜这是预期的行为,而不是错误。所以我在下面添加了这个扩展方法来获得正确的图像数量并且它可以工作。

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

    【讨论】:

    • 完美运行!
    【解决方案2】:

    9223372036854775807 是某些系统上NSNotFound 的值。 PHAssetCollection 的文档提到它可以在无法返回计数时返回 NSNotFound

    如果您只想在必要时进行获取,则应检查NSNotFound

    let smartCollections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)
    smartCollections.enumerateObjectsUsingBlock { object, index, stop in
        guard let collection = object as? PHAssetCollection else { return }
    
        var assetCount = collection.estimatedAssetCount
        if assetCount == NSNotFound {
            let fetchOptions = PHFetchOptions()
            fetchOptions.predicate = NSPredicate(format: "mediaType == %d", PHAssetMediaType.Image.rawValue)
            assetCount = PHAsset.fetchAssetsInAssetCollection(collection, options: fetchOptions).count
        }
    
        print(assetCount)
    }
    

    【讨论】:

      【解决方案3】:

      @Isuru 对 Swift 5 的回答稍作修改

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-25
        • 2017-03-19
        • 2016-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多