【问题标题】:Swift: get assets from Photo Library with excluding subtypesSwift:从照片库中获取不包括子类型的资产
【发布时间】:2019-12-04 11:57:48
【问题描述】:

我想从照片库中获取资产列表,但从 smartAlbumBursts、smartAlbumLivePhotos、smartAlbumScreenshots 等智能文件夹中排除子类型或资产

我的代码是

let options = PHFetchOptions()
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]
options.predicate = predicate
let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)

我试图做这样的谓词:

let predicateType = NSPredicate(format: "mediaSubtypes != %@", 
   PHAssetMediaSubtype.photoScreenshot as! CVarArg)

但是 1. 它崩溃 2. 我只能为截图和 livePhoto 添加 PHAssetMediaSubtype 但不能用于连拍照片。

我知道有方法

if let collection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, 
   subtype: .smartAlbumBursts, options: nil).firstObject {

但我不确定如何为我的目的使用该方法或子类型

正在尝试使用代表Burst,但发生了崩溃:

let predicateType = NSPredicate(format: "representsBurst == %@", NSNumber(value: false))

原因:'获取选项中不支持的谓词:表示Burst == 0'

【问题讨论】:

    标签: ios swift assets phasset photolibrary


    【解决方案1】:

    请参考PHFetchOptions的supported predicate and sort descriptor keys表格。

    我会假设,如果资产不代表爆发,那么它就没有爆发标识符。你可以随意组合:

    let options = PHFetchOptions()
    options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]
    
    // fetch all images with no burstIdentifier
    options.predicate = NSPredicate(format: "burstIdentifier == nil")
    var assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)
    
    // fetch all images with photoScreenshot media subtype
    options.predicate = NSPredicate(format: "((mediaSubtype & %d) != 0)", PHAssetMediaSubtype.photoScreenshot.rawValue)
    assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)
    
    // fetch all images with photoLive media subtype
    options.predicate = NSPredicate(format: "((mediaSubtype & %d) != 0)", PHAssetMediaSubtype.photoLive.rawValue)
    assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)
    
    // fetch all non-photoScreenshot and non-photoLive images
    options.predicate = NSPredicate(format: "NOT (((mediaSubtype & %d) != 0) || ((mediaSubtype & %d) != 0))", PHAssetMediaSubtype.photoScreenshot.rawValue, PHAssetMediaSubtype.photoLive.rawValue)
    assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)
    
    // fetch all non-photoScreenshot and non-photoLive images with no burstIdentifier
    options.predicate = NSPredicate(format: "NOT (((mediaSubtype & %d) != 0) || ((mediaSubtype & %d) != 0)) && burstIdentifier == nil", PHAssetMediaSubtype.photoScreenshot.rawValue, PHAssetMediaSubtype.photoLive.rawValue)
    assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)
    

    【讨论】:

      【解决方案2】:

      NSPredicate 很棘手。说“不”的方式是这样的:

      NSPredicate(
          format: "!((assetCollectionSubtype & %d) == %d)",
              PHAssetCollectionSubtype.smartAlbumBursts.rawValue)
      

      【讨论】:

        猜你喜欢
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        • 2018-08-29
        • 2016-11-12
        • 2015-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多