Why does jpegStillImageNSDataRepresentation throw an exception if the fetch buffer is NOT zero?

On iOS, I use the capture code from AVCaptureStillImageOutput, this way:

[_ captureStillOutput captureStillImageAsynchronouslyFromConnection: _captureConnection completeHandler: asyncCaptureCompletionHandler];

to simplify folding my code, my asyncCaptureCompletionHandler block looks like this:

void(^asyncCaptureCompletionHandler)(CMSampleBufferRef imageDataSampleBuffer, NSError *error) = 
^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
    if (CMSampleBufferIsValid(imageDataSampleBuffer)) {
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];                                                                 
    }
}

I went through all my code and cross-referenced stack overflows and found no suggestion that a valid fetch buffer would be captured without proper JPEG.

_captureStillOutput = [[AVCaptureStillImageOutput alloc] init];
_captureStillOutput.outputSettings = 
        [NSDictionary dictionaryWithObjectsAndKeys:
         AVVideoCodecJPEG, AVVideoCodecKey,
         nil];

if ([session canAddOutput:_captureStillOutput]) {
            [session addOutput:_captureStillOutput];
}

: * - "NSInvalidArgumentException", : "* + [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:] - jpeg. '

google " jpeg" . . .

+5
2

, . , 2015 . , , . , CMSampleBufferRef imageDataSampleBuffer , captureStillImageAsynchronouslyFromConnection.

:

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:connection
                                                       completionHandler:^( CMSampleBufferRef imageDataSampleBuffer, NSError *error )
    {
        //call another method to handle the sample buffer causes weird behaviour
        //maybe the buffer is not being safely referenced by AVFoundation?
        [self handleBufferSomewhereElse:imageDataSampleBuffer]; //will behave strangely
        //even more so if you move to another thread
    }];

:

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:connection
                                                       completionHandler:^( CMSampleBufferRef imageDataSampleBuffer, NSError *error )
    {
        //handle the buffer right here
        NSData *data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
        //works better
    }]; 

, -.

+1

, , :

po imageDataSampleBuffer

, , . , SO, - , , . , , Mac. , . Xcode, .

0

All Articles