视频缩略图需要10-15秒才能显示 [英] Video thumbnail taking 10-15 secs to display
本文介绍了视频缩略图需要10-15秒才能显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用DKImagePickerController从图库中选择视频并尝试显示它的缩略图。不知道为什么,但显示图像需要10-15秒。任何帮助表示赞赏。
I am using DKImagePickerController to select the video from gallery and trying to show a thumbnail of it. Don't know why, but it's taking 10-15 sec to display the image. Any help is appreciated.
以下是代码:
tempDkAsset.fetchAVAssetWithCompleteBlock { (tempVideo, info) in
tempImageView.image = self.thumbnailForVideoAtURL(tempVideo!)
}
func thumbnailForVideoAtURL(_ asset : AVAsset) -> UIImage? {
let assetImageGenerator = AVAssetImageGenerator(asset: asset)
var time = asset.duration
time.value = min(time.value, 2)
do {
let imageRef = try assetImageGenerator.copyCGImage(at: time, actualTime: nil)
return UIImage(cgImage: imageRef)
} catch {
print("error")
return nil
}
}
推荐答案
问题是您在后台线程上调用 thumbnailForVideoAtURL
。你需要在主线程上,因为你正在与界面交谈。
The problem is that you are calling thumbnailForVideoAtURL
on a background thread. You need to be on the main thread because you are talking to the interface.
tempDkAsset.fetchAVAssetWithCompleteBlock { (tempVideo, info) in
DispatchQueue.main.async {
tempImageView.image = self.thumbnailForVideoAtURL(tempVideo!)
}
}
这篇关于视频缩略图需要10-15秒才能显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文