为什么AVCaptureStillImageOutput::captureStillImageAsynchronouslyFromConnection:completionHandler从未被调用?

7
我尝试制作一个应用程序,该应用程序可以捕获iPhone相机的帧并对这些帧进行一些处理。我尝试了一些在互联网上找到的代码片段,例如:这里:如何在iOS中捕获图像而不显示预览
最终,我得到了以下代码:
-(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 将永远不会被调用。 - Boris Prohaska
2个回答

8
我发现在自动引用计数模式下,AVCaptureStillImageOutput 没有正确地保留对 AVCaptureSession 的指针。

这意味着你有两个选择:
  • 禁用自动引用计数,或者

  • 自己保留对 AVCaptureSession 的指针(例如,通过将其分配给控制器中的强属性)。


2
您可能需要检查NSError *error参数,它提供有关成功拍摄图片的信息。AVFoundation错误代码在此处。希望这可以帮助您。

另外,您可能需要阅读这篇文章,其中他解释了在同一位置具有会话代码可能会导致问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接