服务器网址的缩略图

时间:2019-01-19 07:11:35

标签: ios swift

如何从视频URL创建缩略图并加载到表格视图单元格中

我正在使用此代码,但是它冻结了表视图

<body id="body">

</body>

2 个答案:

答案 0 :(得分:0)

您可以尝试使用该代码对您有所帮助

import Foundation
import UIKit
import AVKit

class CommonFunctions {

    static let shared = CommonFunctions()

    typealias ResponseBlock = (_ result: UIImage?) -> Void

    func getVideoThumnail(URLString: String, responseBlock:@escaping ResponseBlock) {
        DispatchQueue.global().async {
            let url = URL(string: URLString)
            let asset = AVAsset(url: url!)
            let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
            assetImgGenerate.appliesPreferredTrackTransform = true
            let time = CMTimeMake(value: 1, timescale: 2)
            let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil)
            if img != nil {
                let frameImg  = UIImage(cgImage: img!)
                DispatchQueue.main.async(execute: {
                    responseBlock(frameImg)
                })
            }else {
                responseBlock(nil)
            }
        }
    }
}

使用:-

 CommonFunctions.shared.getVideoThumnail(URLString: downloadURL) { (image) in
                                if image != nil {
                                      let thumbimage = image
                                }
                            }

答案 1 :(得分:0)

UIThread

中使用您的方法
DispatchQueue.main.async() {
    // your UI update code
}

它可以防止冻结。