Roi Mulia Roi Mulia - 1 year ago 144
Swift Question

PHFetchOptions Duration not working properly

I'm trying to fetch all the Videos which their duration is higher than 1.5 sec.

What i have tried

let videoOptions = PHFetchOptions()
let dur : Double = 1.5
videoOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
videoOptions.predicate = NSPredicate(format: "mediaType = %d AND duration > %d", PHAssetMediaType.Video.rawValue,dur)
VideoCollectionFetchResult = PHAsset.fetchAssetsWithOptions(videoOptions)

The docs says :

enter image description here

Any suggestions? Thanks!

Answer Source

It is related to the format - duration is actually Double format, and %d is for integer. Try to use %f, like:

(format: "mediaType = %d AND duration > %f", PHAssetMediaType.Video.rawValue,dur)