Roi Mulia Roi Mulia - 6 months ago 36x
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!


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)