Swift 通过PHAsset拍摄实时照片和普通照片
我想展示一组照片。为了获得这些照片,我使用了Swift 通过PHAsset拍摄实时照片和普通照片,swift,photos,Swift,Photos,我想展示一组照片。为了获得这些照片,我使用了Photos框架 我使用以下代码获取照片: let options = PHFetchOptions() options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ] images = PHAsset.fetchAssets(with: .image, options: options) 然而,我也希望得到实时照片(所以两者的结合,
Photos
框架
我使用以下代码获取照片:
let options = PHFetchOptions()
options.sortDescriptors = [
NSSortDescriptor(key: "creationDate", ascending: false)
]
images = PHAsset.fetchAssets(with: .image, options: options)
然而,我也希望得到实时照片(所以两者的结合,实时照片最好只是第一个静止帧)
现在我知道你可以得到这样的现场照片:
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
获取实时照片和静态照片的方法:
步骤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
第五步:享受
因此,在你的情况下:
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])
以下是通过
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
第五步:享受
因此,在你的情况下:
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])
一句话,你应该使用andPredicateWithSubpredicates而不是orPredicateWithSubpredicates来成功过滤实时照片。一句话,你应该使用andPredicateWithSubpredicates而不是orPredicateWithSubpredicates来成功过滤实时照片。