1.导入 AVFoundation库
import AVFoundation
2.核心代码
var videoUrl: String? {
didSet {
guard let url = videoUrl else {
return
}
DispatchQueue.global(qos: .utility).async {
do {
let asset = AVURLAsset.init(url: URL(string: url)!)
let gen = AVAssetImageGenerator.init(asset: asset)
gen.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(0.0, preferredTimescale: 1)
var actualTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale: 0)
let image = try gen.copyCGImage(at: time, actualTime: &actualTime)
DispatchQueue.main.async {
self.videoV.image = UIImage.init(cgImage: image)
}
} catch {
print("错误")
}
}
}
}
网友评论