PHFetchOptions持续时间无法正常工作

时间:2016-04-19 12:22:32

标签: objective-c swift nspredicate phasset phfetchoptions

我试图获取持续时间超过1.5秒的所有视频

我尝试了什么

        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) 

文档说:

enter image description here

有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分:2)

与格式有关 - 持续时间实际上是Double格式,%d是整数。尝试使用%f,例如:

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

答案 1 :(得分:1)

Kateryna的回答很好,但还没有完成代码。因此,不是花时间一起解析答案,而是这里的工作代码:

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