Swift: 如何设置两个日期之间的照片范围?

Swift: how to set range of photos between 2 dates?

我想从照片库中获取 1 月 1 日至 5 月 1 日的照片 我该怎么做

我的代码适用于所有照片:

let options = PHFetchOptions()
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]
options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
let assets = PHAsset.fetchAssets(with: options)

有一个 creationDate 属性,因此您可以扩展谓词以过滤日期:

(creationDate >= %@) AND (creationDate <= %@)

因此您的谓词应如下所示

options.predicate = NSPredicate(
  format: "mediaType = %d AND (creationDate >= %@) AND (creationDate <= %@)", 
  PHAssetMediaType.image.rawValue, 
  fromDate as NSDate, 
  toDate as NSDate
)