Video thumbnail taking 10-15 secs to display

2019-03-06 04:53发布

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
    }
}

1条回答
等我变得足够好
2楼-- · 2019-03-06 05:17

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!)
    }
}  
查看更多
登录 后发表回答