2013-02-04 15 views
7

私はiPhoneカメラからフレームをキャプチャし、これらのフレームで何らかの処理を行うアプリケーションを構築しようとしています。私は、インターネット上で見つかったいくつかのコードを試しました。ここに:How to capture image without displaying preview in iOSなぜAVCaptureStillImageOutput :: captureStillImageAsynchronouslyFromConnection:completionHandlerは呼び出されませんか?

と、私は次のコードになってしまった:

-(IBAction)captureNow{ 
    AVCaptureConnection *videoConnection = nil; 
    for (AVCaptureConnection *connection in stillImageOutput.connections) 
    { 
     for (AVCaptureInputPort *port in [connection inputPorts]) 
     { 
      if ([[port mediaType] isEqual:AVMediaTypeVideo]) 
      { 
       videoConnection = connection; 
       break; 
      } 
     } 
     if (videoConnection) { break; } 
    } 

    // NSLog(@"about to request a capture from: %@", stillImageOutput); 
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) 
    { 
     CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); 
     if (exifAttachments) 
     { 
      // Do something with the attachments. 
      NSLog(@"attachements: %@", exifAttachments); 
     } 
     else 
      NSLog(@"no attachments"); 

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
     UIImage *image = [[UIImage alloc] initWithData:imageData]; 

     // do the processing with the image  
     } 

しかし、アプリはので、私はフレームから画像を取得することはありませんcaptureStillImageAsynchronouslyFromConnection方式のハンドラのコードブロックに入ったことはありません。 私は間違ったやり方で何かしていますか? お寄せいただきありがとうございます!

+2

あなたの 'AVCaptureSession'がまだ動いているかどうか確認するかもしれません。早く終了すると、completionHandlerが呼び出されることはありません。 –

答えて

8

私は、AVCaptureSessionへのポインタが自動参照カウントモードでAVCaptureStillImageOutputによって正しく保持されないことを発見しました。 (お使いのコントローラに強いプロパティに割り当てることによって、例えば)AVCaptureSessionに自分自身を指すポインタを保持

2

NSError *エラーパラメータを確認するとよいでしょう。これは、撮影に成功した写真に関する情報を提供します。 AVFoundationエラーコードはhereです。私はこれが役立つことを願っています

また、thisについては、同じ場所でセッションコードを使用すると問題が発生する可能性があることについて説明しています。

関連する問題