如何从视频中获取单个图像?

时间:2014-09-14 10:02:08

标签: ios objective-c image video

我已经查了很多代码,但我还没有找到合适的解决方案。我正在使用Xcode 5.1.1和Iphone Retina 4inch。我希望通过点击它从视频中获取单个图像。之后,我将编辑图像并将效果应用于整个视频。谢谢。

更新: 我发现此代码的目的与此相同。它也没有在模拟器上工作。有人可以告诉我这是什么问题吗?

 -(UIImage *)generateThumbImage : (NSString *)filepath
{
NSURL *url = [NSURL fileURLWithPath:filepath];

AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = [asset duration];
time.value = 0;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);  // CGImageRef won't be released by ARC

return thumbnail;

}

2 个答案:

答案 0 :(得分:1)

此代码将从加载的视频中的特定时间生成图像,并将图像从CGImage转换为UIImage。

对代码进行了注释以解释其各个部分。

- (UIImage*)loadImage {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil];

    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset]; // Create object of Video. 

    NSError *err = NULL;

    CMTime time = CMTimeMake(1, 60); // Select time of 1/60ths of a second. 

    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err]; // Get Image on that time frame. 

    NSLog(@"err==%@, imageRef==%@", err, imgRef); // if something is not as expected then u can log error. 

    return [[UIImage alloc] initWithCGImage:imgRef]; // Convert Image from CGImage to UIImage so you can display it easily in a image view. 

}

答案 1 :(得分:0)

您是否尝试过AVFoundation框架?

这就是它的用途。该文档位于Apple网站上:

  

AV Foundation框架提供了与之合作的基本服务   iOS和OS X上基于时间的视听媒体。通过现代   Objective-C界面,可以轻松播放,捕捉,编辑或编码   媒体格式,如QuickTime电影和MPEG-4文件。