我正在尝试显示一组照片。为了获得这些照片,我正在使用该Photos框架。
我使用以下代码来获取照片:
let options = PHFetchOptions()
options.sortDescriptors = [
NSSortDescriptor(key: "creationDate", ascending: false)
]
images = PHAsset.fetchAssets(with: .image, options: options)
Run Code Online (Sandbox Code Playgroud)
但是,我也想获得 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)
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何将两者结合起来......有没有办法做到这一点,也许是通过创建两个NSPredicates?
谢谢
以下是通过以下方式获取实时照片和静态照片的方法PHAsset:
步骤1:创建一个NSPredicate检测正常照片:
let imagesPredicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
Run Code Online (Sandbox Code Playgroud)
第 2 步:创建一个NSPredicate以检测实时照片:
let liveImagesPredicate = NSPredicate(format: "(mediaSubtype & %d) != 0", PHAssetMediaSubtype.photoLive.rawValue)
Run Code Online (Sandbox Code Playgroud)
第 3 步:将两者与 a 结合NSCompoundPredicate:
let compound = NSCompoundPredicate(orPredicateWithSubpredicates: [imagesPredicate, liveImagesPredicate])
Run Code Online (Sandbox Code Playgroud)
第 4 步:将 分配NSCompoundPredicate给您的PHFetchOptions:
options.predicate = compound
Run Code Online (Sandbox Code Playgroud)
第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])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1362 次 |
| 最近记录: |