1

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!

4

2 回答 2

2

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)
于 2016-04-19T16:03:41.357 回答
1

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
于 2016-12-06T18:32:01.233 回答