当样本缓冲区为非null时,为什么jpegStillImageNSDataRepresentation会抛出异常?

Tom*_*ace 5 ios avcapture

在iOS中,我使用代码从AVCaptureStillImageOutput捕获:

[_captureStillOutput captureStillImageAsynchronouslyFromConnection:_captureConnection completionHandler:asyncCaptureCompletionHandler];

为了简单起见我的代码,我的asyncCaptureCompletionHandler块看起来像这样:

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

我已经浏览了所有代码并交叉引用堆栈溢出,并且没有找到任何建议为什么在没有正确的JPEG的情况下捕获有效的样本缓冲区.

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

if ([session canAddOutput:_captureStillOutput]) {
            [session addOutput:_captureStillOutput];
}
Run Code Online (Sandbox Code Playgroud)

调试器中有补充信息: *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* + [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:] - 不是jpeg示例缓冲区.

搜索谷歌和堆栈溢出"Not a jpeg sample buffer"产生零结果.我被卡住了.呸.

Tom*_*ace 0

该解决方案的下一步是使用以下命令记录调试器中报告的所有数据:

po imageDataSampleBuffer
Run Code Online (Sandbox Code Playgroud)

在抛出异常时,这总是会产生大量详细信息,即样本缓冲区上的大量信息。然后自从将其发布到 SO 后,我注释掉了一些代码,然后取消注释它,现在它正在工作。我的代码没有任何变化,但是,我确实关闭了 mac 上运行的一些程序。也许这是一个开发机器错误。之后,我关闭并重新打开Xcode,并且没有抛出异常。