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.
Here's the code:
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
}
}
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!)
}
}