从UIImagePickerController抓取视频的第一帧? [英] Grabbing the first frame of a video from UIImagePickerController?

查看:663
本文介绍了从UIImagePickerController抓取视频的第一帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 UIImagePickerController 中的所选视频中获取第一帧以显示在 UIImageView 中,但我不知道是否可能。如果是,我该怎么做?

I'm trying to get the first frame from the selected video in a UIImagePickerController to show in a UIImageView, but I do not know if it's possible. If it is, how would I do it?

推荐答案

您可以通过以下两种方式之一完成此操作。第一种方法是使用 MPMoviePlayerController 来获取缩略图:

You can do this in one of two ways. The first way is to use the MPMoviePlayerController to grab the thumbnail:

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
                                       initWithContentURL:videoURL];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time
                     timeOption:MPMovieTimeOptionNearestKeyFrame];

这样可行,但 MPMoviePlayerController 不是特别轻量级的对象并没有特别快速抓取缩略图。

This works, but MPMoviePlayerController is not a particularly lightweight object and not particularly fast grabbing thumbnails.

首选方法是在AVFoundation中使用新的 AVAssetImageGenerator 。这比旧方式快速,轻便且灵活。这是一个帮助方法,它将从视频中返回一个自动释放的图像。

The preferred way is to use the new AVAssetImageGenerator in AVFoundation. This is fast, lightweight and more flexible than the old way. Here's a helper method that will return an autoreleased image from the video.


+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL 
                             atTime:(NSTimeInterval)time 
{

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetIG = 
                [[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetIG.appliesPreferredTrackTransform = YES;
    assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = time;
    NSError *igError = nil;
    thumbnailImageRef = 
             [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
                             actualTime:NULL
                                  error:&igError];

    if (!thumbnailImageRef)
        NSLog(@"thumbnailImageGenerationError %@", igError );

    UIImage *thumbnailImage = thumbnailImageRef 
                          ? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
                          : nil;

    return thumbnailImage;
}

异步使用


- (void)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time completion:(void (^)(UIImage *)) completion
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
        NSParameterAssert(asset);
        AVAssetImageGenerator *assetIG =
        [[AVAssetImageGenerator alloc] initWithAsset:asset];
        assetIG.appliesPreferredTrackTransform = YES;
        assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

        CGImageRef thumbnailImageRef = NULL;
        CFTimeInterval thumbnailImageTime = time;
        NSError *igError = nil;
        thumbnailImageRef =
        [assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
                        actualTime:NULL
                             error:&igError];

        if (!thumbnailImageRef)
            NSLog(@"thumbnailImageGenerationError %@", igError );

        UIImage *thumbnailImage = thumbnailImageRef
        ? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
        : nil;

        dispatch_async(dispatch_get_main_queue(), ^{
            completion(thumbnailImage);
        });
    });
}

这篇关于从UIImagePickerController抓取视频的第一帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆