如何从本地视频文件在swift中创建缩略图?

例如,如果视频文件路径位于此处:

file:///Users/Dev/Library/Developer/CoreSimulator/Devices/F33222DF-D8F0-448B-A127-C5B03C64D0DC/data/Containers/Data/Application/4BC62DBF-0108-453C-9324-5BC0E356FE24/tmp/trim.059D11E6-F0EF-43DB-9E97-CA4F1F95D6B6.MOV

谢谢您.

推荐答案

翻译自:

First frame of a video using AVFoundation

    var err: NSError? = nil
    let asset = AVURLAsset(URL: NSURL(fileURLWithPath: "/that/long/path"), options: nil)
    let imgGenerator = AVAssetImageGenerator(asset: asset)
    let cgImage = imgGenerator.copyCGImageAtTime(CMTimeMake(0, 1), actualTime: nil, error: &err)
    // !! check the error before proceeding
    let uiImage = UIImage(CGImage: cgImage)
    let imageView = UIImageView(image: uiImage)
    // lay out this image view, or if it already exists, set its image property to uiImage

Swift相关问答推荐

AirPods手势不发送AVAudioApp静音状态通知

计算Vision OS相机与Vision OS中3D模型之间的距离

运行异步任务串行运行

Swift C外部实现的Task / Future类类型

是否可以循环遍历SWIFT子类并访问它们的静态变量S?

无法创建MKAssociateRegion对象

如何使泡泡上的箭头直达封闭矩形

从`compactMap()`闭包返回`nil`未通过结果类型判断

如何在SwiftUI中做出适当的曲线?

如何观察UIViewRepresentable中的多个变化?

String(validatingUTF8:) 和 String(utf8String:) 之间有区别吗?

UIButton 配置不更新按钮标题

数组使用偏移量对自身执行 XOR

在 SwiftUI 中操作绑定变量

来自数据的 SwiftUI 图像

阻止其他类型的键盘

找不到接受提供的参数的/的重载

在运行时访问 UIView 宽度

根据禁用与否更改 SwiftUI 中按钮的 colored颜色

Swift performSegueWithIdentifier 不起作用