4

I am integrating custom image picker and i have to show videos from camera roll whose duration is less than 10 seconds. Below code fetches all videos from gallery but i want to apply predicate to filter based on duration as well.

let options = PHFetchOptions()
    options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true) ]
    options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
    assets = PHAsset.fetchAssets(with: options)
    print(assets ?? "no video found")
    collectionView.reloadData()

Please let me know if anyone has any idea regarding the same. Thanks.

khushboo
  • 260
  • 2
  • 12

1 Answers1

8

Just add the condition to the predicate

options.predicate = NSPredicate(format: "mediaType = %d AND duration < 10", PHAssetMediaType.video.rawValue)
vadian
  • 274,689
  • 30
  • 353
  • 361