问题描述
我正在从视频中检索一些UIImage,并将它们存储在数组中。
fileprivate func createImageFramesFromVideo(urlVideoPassed: URL,completion: @escaping ([UIImage]) -> Void) {
let finalAsset = AVAsset(url: urlVideoPassed)
let imageGenerator = AVAssetImageGenerator(asset: finalAsset)
let videoDuration = asset!.duration
//getting image snapshots more or less in half the video
let time1 = CMTimeMakeWithSeconds(videoDuration.seconds/2-(videoDuration.seconds*0.3),preferredTimescale: videoDuration.timescale)
let time2 = CMTimeMakeWithSeconds(videoDuration.seconds/2-(videoDuration.seconds*0.2),preferredTimescale: videoDuration.timescale)
let time3 = CMTimeMakeWithSeconds(videoDuration.seconds/2-(videoDuration.seconds*0.1),preferredTimescale: videoDuration.timescale)
let time4 = CMTimeMakeWithSeconds(videoDuration.seconds/2,preferredTimescale: videoDuration.timescale)
let time5 = CMTimeMakeWithSeconds(videoDuration.seconds/2+(videoDuration.seconds*0.1),preferredTimescale: videoDuration.timescale)
let time6 = CMTimeMakeWithSeconds(videoDuration.seconds/2+(videoDuration.seconds*0.2),preferredTimescale: videoDuration.timescale)
let time7 = CMTimeMakeWithSeconds(videoDuration.seconds/2+(videoDuration.seconds*0.3),preferredTimescale: videoDuration.timescale)
let times = [NSValue(time: time1),NSValue(time: time2),NSValue(time: time3),NSValue(time: time4),NSValue(time: time5),NSValue(time: time6),NSValue(time: time7)]
var imageArray : [UIImage] = []
let dispatchGroup = DispatchGroup()
times.forEach { _ in
dispatchGroup.enter()
}
imageGenerator.generateCGImagesAsynchronously(forTimes: times) { (time,image,time1,result,err) in
if let err = err {
print("there's an error in retrieving the images",err)
}
guard let imageTopass = image else {
return
}
let theImage = UIImage(cgImage: imageTopass)
imageArray.append(theImage)
dispatchGroup.leave()
}
//when all images are retrieved,passes them to completion block
dispatchGroup.notify(queue: .main) {
completion(imageArray)
}
}
问题在于时间戳:
let time3 = CMTimeMakeWithSeconds(videoDuration.seconds/2-(videoDuration.seconds*0.1),preferredTimescale: videoDuration.timescale)
和
let time5 = CMTimeMakeWithSeconds(videoDuration.seconds/2+(videoDuration.seconds*0.1),preferredTimescale: videoDuration.timescale)
即使在某种情况下我增加了一些时间,在另一种情况下我减去了它,我也会检索同一帧。对于其他所有具有相反符号的帧,也会发生这种情况。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)