2012-09-10 43 views
5

在iOS中,我使用的代码从AVCaptureStillImageOutput从而捕捉:为什么样本缓冲区不为空时jpegStillImageNSDataRepresentation会引发异常?

[_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样品缓冲液'。

谷歌和堆栈溢出搜索“不是一个jpeg样本缓冲区”产生零结果。我卡住了。呸。

回答

0

该解决方案的下一步是使用登录调试器报告的所有数据:

po imageDataSampleBuffer 

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

+0

可以确认,一个好的“rm -rf DerivedData/*”并重新启动Xcode为我解决了这个问题。 – Austin

+1

事实上,问题再次出现,但当我关闭iTunes后便消失了。这是一个非常疯狂的 – Austin

+0

谢谢你的注意,它再次出现,然后再次消失。 –

1

这个问题是古老的,但黄金。我来自未来,可以确认这仍然发生在2015年。我试图通过相同的步骤来解决问题,但无济于事。但是,这个问题让我意识到,CMSampleBufferRef imageDataSampleBuffercaptureStillImageAsynchronouslyFromConnection的完成处理程序之外处理时表现异常。

一言以蔽之:

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

希望这可以帮助别人。

相关问题