![](/img/trans.png)
[英]PHFetchOptions() Photos Only Using PHAsset & PHAssetCollection
[英]Live Photos and normal photos through PHAsset
我正在嘗試顯示照片集。 要獲取這些照片,我正在使用“ Photos
框架。
我正在使用以下代碼來獲取照片:
let options = PHFetchOptions()
options.sortDescriptors = [
NSSortDescriptor(key: "creationDate", ascending: false)
]
images = PHAsset.fetchAssets(with: .image, options: options)
但是,我也想獲得Live Photo的(因此,將Live Photo的兩者結合起來,理想情況下將是第一個靜止幀。
現在,我知道您可以像這樣獲取實時照片:
let options = PHFetchOptions()
options.sortDescriptors = [
NSSortDescriptor(key: "creationDate", ascending: false)
]
options.predicate = NSPredicate(format: "(mediaSubtype & %d) != 0", PHAssetMediaSubtype.photoLive.rawValue)
images = PHAsset.fetchAssets(with: options)
但是,我不知道如何將兩者結合起來。有沒有辦法做到這一點,也許可以通過創建兩個NSPredicate
s來實現?
謝謝
通過PHAsset
獲取實時照片和靜態照片的方法PHAsset
:
步驟1:創建NSPredicate
來檢測普通照片:
let imagesPredicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
步驟2:創建NSPredicate
來檢測實時照片:
let liveImagesPredicate = NSPredicate(format: "(mediaSubtype & %d) != 0", PHAssetMediaSubtype.photoLive.rawValue)
步驟3:將兩者與NSCompoundPredicate
結合:
let compound = NSCompoundPredicate(orPredicateWithSubpredicates: [imagesPredicate, liveImagesPredicate])
步驟4:將NSCompoundPredicate
分配給您的PHFetchOptions
:
options.predicate = compound
步驟5:盡情享受!
因此,在您的情況下:
let options = PHFetchOptions()
options.sortDescriptors = [
NSSortDescriptor(key: "creationDate", ascending: false)
]
// Get all still images
let imagesPredicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
// Get all live photos
let liveImagesPredicate = NSPredicate(format: "(mediaSubtype & %d) != 0", PHAssetMediaSubtype.photoLive.rawValue)
// Combine the two predicates into a statement that checks if the asset
// complies to one of the predicates.
options.predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [imagesPredicate, liveImagesPredicate])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.