I need to obtain the UIImage
from uncompressed image data from CMSampleBufferRef. I'm using the code:
captureStillImageOutput captureStillImageAsynchronouslyFromConnection:connection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
// that famous function from Apple docs found on a lot of websites
// does NOT work for still images
UIImage *capturedImage = [self imageFromSampleBuffer:imageSampleBuffer];
}
http://developer.apple.com/library/ios/#qa/qa1702/_index.html is a link to imageFromSampleBuffer
function.
But it does not work properly. :(
There is a jpegStillImageNSDataRepresentation:imageSampleBuffer
method, but it gives the compressed data (well, because JPEG).
How can I get UIImage
created with the most raw non-compressed data after capturing Still Image?
Maybe, I should specify some settings to video output? I'm currently using those:
captureStillImageOutput = [[AVCaptureStillImageOutput alloc] init];
captureStillImageOutput.outputSettings = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
I've noticed, that output has a default value for AVVideoCodecKey
, which is AVVideoCodecJPEG
. Can it be avoided in any way, or does it even matter when capturing still image?
I found something there: Raw image data from camera like "645 PRO" , but I need just a UIImage, without using OpenCV or OGLES or other 3rd party.
Here's a more efficient way:
The method
imageFromSampleBuffer
does work in fact I'm using a changed version of it, but if I remember correctly you need to set the outputSettings right. I think you need to set the key askCVPixelBufferPixelFormatTypeKey
and the value askCVPixelFormatType_32BGRA
.So for example:
EDIT
I am using those settings to take stillImages not video. Is your sessionPreset AVCaptureSessionPresetPhoto? There may be problems with that
EDIT 2
The part about saving it to UIImage is identical with the one from the documentation. That's the reason I was asking for other origins of the problem, but I guess that was just grasping for straws. There is another way I know of, but that requires OpenCV.
I guess that is of no help to you, sorry. I don't know enough to think of other origins for your problem.