【问题标题】:Why does jpegStillImageNSDataRepresentation throw an exception when sample buffer is NOT null?为什么当样本缓冲区不为空时 jpegStillImageNSDataRepresentation 会抛出异常?
【发布时间】:2012-09-10 16:28:10
【问题描述】:

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

我已经检查了我的所有代码并交叉引用堆栈溢出,但没有找到任何建议,为什么会在没有正确 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 样本缓冲区”产生的结果为零。我被困住了。呸。

【问题讨论】:

    标签: ios avcapture


    【解决方案1】:

    这个问题很老但是很金。我来自未来,可以确认这仍然发生在 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
        }]; 
    

    希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

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

      po imageDataSampleBuffer
      

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

      【讨论】:

      • 可以确认,很好的rm -rf DerivedData/* 并重新启动 Xcode 为我解决了这个问题。
      • 实际上,问题再次出现,但在我关闭 iTunes 后又消失了。这是一个非常疯狂的
      • 感谢您注意它再次出现然后又消失了。
      猜你喜欢
      • 1970-01-01
      • 2020-09-30
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2022-09-30
      • 2021-03-04
      • 1970-01-01
      • 2011-10-13
      相关资源
      最近更新 更多