如何从录制的视频-AVFoundation获取帧作为图像(图像作为缩略图)

时间:2013-05-01 06:29:24

标签: iphone ios objective-c avfoundation

我想从录制的视频中提取图像。我正在使用这段代码,但没有结果,图片没有出现。

我正在使用以下代码执行此任务

AVURLAsset* asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:videoPath] options:nil];
AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
UIImage* image = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];
[videoFrame setImage:image];

请帮助使用代码来从录制的视频中提取图像

2 个答案:

答案 0 :(得分:5)

试试这个

    NSURL *videoURl = [NSURL fileURLWithPath:videoPath];
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generate.appliesPreferredTrackTransform = YES;
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
     UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
    [videoFrame setImage:img];
    [img release];

答案 1 :(得分:0)

使用此方法生成缩略图:

#pragma mark -
#pragma mark THUMBNAIL GENARATION
- (UIImage*)testGenerateThumbNailDataWithVideo:(NSURL *)videoURL {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;

    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *currentImg = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);

    return currentImg;
}

希望它对你有所帮助。