ios - 从 Swift 3.0 中的服务器视频 URL 获取缩略图/预览图像

标签 ios swift swift3

我想要来自服务器底层视频的缩略图。视频文件不在本地。它在我的服务器上。视频文件的扩展名为 .m3u8

最佳答案

你可以的。

第一步:您需要导入AVFoundation:

    import AVFoundation

然后将下面的代码添加到您的 Controller 中:

func getThumbnailImage(forUrl url: URL) -> UIImage? {
    let asset: AVAsset = AVAsset(url: url)
    let imageGenerator = AVAssetImageGenerator(asset: asset)

    do {
        let thumbnailImage = try imageGenerator.copyCGImage(at: CMTimeMake(value: 1, timescale: 60), actualTime: nil)
        return UIImage(cgImage: thumbnailImage)
    } catch let error {
        print(error)
    }

    return nil
}

用法:

    let imageView = UIImageView()
    let url = URL(string: "your_video_url")

    if let thumbnailImage = getThumbnailImage(forUrl: url) {
        imageView.image = thumbnailImage
    }

url 更改为您的视频链接。

关于ios - 从 Swift 3.0 中的服务器视频 URL 获取缩略图/预览图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129816/

相关文章:

Swift 3 不正确的字符串插值与隐式展开的 Optionals

ios - 有没有人看到,警告: target specifies SWIFT_VERSION = '2.3'

ios - 使用 requestLocation 仅请求一次用户位置

ios - Alamofire laravel 中字符 0 周围的无效值

ios - 单击搜索栏不会提示键盘(iOS Swift)

ios - Swift 唯一防止 NSKeyedUnarchiver.decodeObject 崩溃的方法?

ios - 将数组 int 附加到字符串数组

iphone - 使用NSSortDescriptor对特定项目进行排序的数组

ios - 使用 OneDrive 对用户进行身份验证后,我将如何使用 REST 框架获取他们的 Xbox live 玩家代号?

ios - NSURLSession 多部分/表单数据发布请求出现问题