2013-02-04 98 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的信息,他解释说,在同一地点使用会话代码可能会导致问题。

相关问题