Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 通过PHAsset拍摄实时照片和普通照片_Swift_Photos - Fatal编程技术网

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来成功过滤实时照片。