MPMoviePlayerController

MPMoviePlayerController

本文介绍了从iPhone SDK中的视频URL创建缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从视频URL获取缩略图.视频是m3u8格式的流(HLS).我已经从MPMoviePlayerController尝试了requestThumbnailImagesAtTimes,但是没有用.有人有解决这个问题的办法吗?如果是这样,您怎么做?

I am trying to acquire a thumbnail from a video url. The video is a stream (HLS) with the m3u8 format.I've already tried requestThumbnailImagesAtTimes from the MPMoviePlayerController, but that didn't work. Does anyone have a solution for that problem? If so how'd you do it?

推荐答案

如果您不想使用MPMoviePlayerController,则可以执行以下操作:

If you don't want to use MPMoviePlayerController, you can do this:

    AVAsset *asset = [AVAsset assetWithURL:sourceURL];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    CMTime time = CMTimeMake(1, 1);
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);  // CGImageRef won't be released by ARC

这是Swift中的一个示例:

Here's an example in Swift:

func thumbnail(sourceURL sourceURL:NSURL) -> UIImage {
    let asset = AVAsset(URL: sourceURL)
    let imageGenerator = AVAssetImageGenerator(asset: asset)
    let time = CMTime(seconds: 1, preferredTimescale: 1)

    do {
        let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
        return UIImage(CGImage: imageRef)
    } catch {
        print(error)
        return UIImage(named: "some generic thumbnail")!
    }
}

MPMoviePlayerController相比,我更喜欢使用AVAssetImageGenerator,因为它是线程安全的,并且一次可以实例化多个.

I prefer using AVAssetImageGenerator over MPMoviePlayerController because it is thread-safe, and you can have more than one instantiated at a time.

这篇关于从iPhone SDK中的视频URL创建缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:33