PHFetchOptions Duration not working properly

804 Views Asked by At

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!

2

There are 2 best solutions below

0
On BEST ANSWER

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)
0
On

Kateryna's answer is fine but it's not completed code. So instead of spending time parsing the answer together, here's working code:

let videoOptions = PHFetchOptions()
videoOptions.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]
videoOptions.predicate = NSPredicate(format: "mediaType = %d AND duration > %f", PHAssetMediaType.video.rawValue, 1.5) // only video and 1.5 seconds or more

return videoOptions