PHFetchOptions 持续时间无法正常工作

PHFetchOptions Duration not working properly

我正在尝试获取所有时长超过 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) 

文档说:

有什么建议吗?谢谢!

与格式有关——duration实际上是Double格式,%d是整数。尝试使用 %f,例如:

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

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