ios - Swift:从照片库中获取资源,排除子类型

标签 ios swift assets phasset photolibrary

我想从照片库中获取 Assets 列表,但从智能文件夹中排除子类型或 Assets ,例如 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,但不能为连拍照片添加 PHAssetMediaSubtype。

我知道有方法

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

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

尝试使用representsBurst但发生崩溃:

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

reason: 'Unsupported predicate in fetch options: representsBurst == 0'

最佳答案

请引用supported predicate and sort descriptor keys表格PHFetchOptions

我会假设,如果 Assets 不代表爆发,那么它就没有爆发标识符。您可以根据需要组合它:

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)

关于ios - Swift:从照片库中获取资源,排除子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57214953/

相关文章:

java - Assets 目录内的 Dropwizard 服务目录

angular - 使用 Webpack 5 模块联合提供样式和 Assets

ios - 在 Flutter 中打开原生 UIViewController

ios - 更智能的自动资本化

ios - 使用 Parse 从通讯录添加好友

swift - swift 3.0中跟踪一批HTTP请求的解决方案

ios - 核心动画奇怪显示后 addSubview 然后 removeFromSuperview

ios - 如何在同一个iOS项目中使用多个Icomoon字体?

ios - 在 UINavigationController 中嵌入 View Controller

Sinatra 中的 css.erb 文件