UIImagePickerController:从视频中获取图像 [英] UIImagePickerController: Getting image capture from video

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

问题描述

我正在尝试从视频中提取帧作为图像。视频是使用UIImagePickerController录制的。

I'm trying to extract a frame from a video as an image. The video is recorded using UIImagePickerController.

录制视频后,我会获取其URL并使用AVURLAsset加载它。然后我创建一个AVAssetReader和AVAssetReaderTrackOutput来获取各个帧。

When the video has been recorded I get its URL and load it using AVURLAsset. Then I create a AVAssetReader and AVAssetReaderTrackOutput to get the individual frames.

当我将帧作为CMSampleBufferRef时,我将它们传递给Apple的imageFromSampleBuffer方法,该方法应该返回一个UIImage。当我使用AVCaptureSession获取帧时,此方法工作正常但当我使用通过UIImagePickerController录制的视频时,此行返回0x0:

When I get the frames as CMSampleBufferRef's I pass them to Apple's imageFromSampleBuffer method which should return a UIImage. This method worked fine when I was getting frames using an AVCaptureSession but when I use a video recorded via UIImagePickerController then this line returns 0x0:

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

其中sampleBuffer是我传递的CMSampleBufferRed。

Where sampleBuffer is the CMSampleBufferRed that I pass.

我通过调试器检查了sampleBuffer的值,看起来没问题(不是0x0)。 CMSampleBufferGetImageBuffer会返回0x0是否有任何原因?

I checked the value of sampleBuffer via the debugger and it looked ok (wasn't 0x0). Is there any reason why CMSampleBufferGetImageBuffer would return 0x0?

或者是否有其他方法从MOV文件中提取单个视频帧并将其保存为UIImage?

Or alternatively is there another way to extract a single video frame from a MOV file and save it as a UIImage?

谢谢。

推荐答案

我找到了解决方案。您必须在初始化AVAssetReaderTrackOutput时设置的设置中设置像素格式。

I found the solution. You have to set the pixel format in the settings passed when you initialize an AVAssetReaderTrackOutput.

我通过了以下设置字典:

I passed the following settings dictionary:

NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 

希望这有助于其他人。

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

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