Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerController: Getting image capture from video

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

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.

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); 

Where sampleBuffer is the CMSampleBufferRed that I pass.

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?

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

Thanks.

like image 354
nebs Avatar asked Feb 16 '11 22:02

nebs


1 Answers

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]; 

Hope this helps someone else.

like image 52
nebs Avatar answered Sep 20 '22 20:09

nebs