2013-02-04 102 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正確保留。

這意味着你有兩個選擇:

2

您可能想要檢查NSError *錯誤參數。它爲您提供有關正在成功拍攝的照片的信息。 AVFoundation錯誤代碼是here。我希望這有幫助。

另外,您也可能想要了解有關this的信息,他解釋說,在同一地點使用會話代碼可能會導致問題。

相關問題