2012-09-10 4 views
5

、私は、このようAVCaptureStillImageOutputからキャプチャするためのコードを使用しています:サンプルバッファがヌルでない場合、なぜjpegStillImageNSDataRepresentationが例外をスローしますか? IOSの

[_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) 
    { 
     //handle the buffer right here 
     NSData *data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
     //works better 
    }]; 

・ホープこれは誰かに役立ちます:

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

代わりにこれをやって好みます。

関連する問題