【问题标题】:Video thumbnail taking 10-15 secs to display显示视频缩略图需要 10-15 秒
【发布时间】:2017-05-15 09:32:15
【问题描述】:

我正在使用 DKImagePickerController 从图库中选择视频并尝试显示它的缩略图。不知道为什么,但显示图像需要 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
    }
}

【问题讨论】:

    标签: ios iphone swift xcode swift3


    【解决方案1】:

    问题是您在后台线程上调用thumbnailForVideoAtURL。您需要在主线程上,因为您正在与界面对话。

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

    【讨论】:

    • 我完全忘记了块在后台线程上运行。谢谢:)
    猜你喜欢
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2020-05-30
    • 1970-01-01
    相关资源
    最近更新 更多