video缩略图需要10-15秒才能显示

我正在使用DKImagePickerController从图库中selectvideo,并尝试显示它的缩略图。 不知道为什么,但需要10-15秒才能显示图像。 任何帮助表示赞赏。

代码如下:

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 。 您需要在主线程上,因为您正在与界面交谈。

 tempDkAsset.fetchAVAssetWithCompleteBlock { (tempVideo, info) in DispatchQueue.main.async { tempImageView.image = self.thumbnailForVideoAtURL(tempVideo!) } }