【问题标题】:Why AVCaptureStillImageOutput ::captureStillImageAsynchronouslyFromConnection:completionHandler is never called?为什么 AVCaptureStillImageOutput ::captureStillImageAsynchronouslyFromConnection:completionHandler 永远不会被调用?
【发布时间】:2013-02-04 15:58:29
【问题描述】:

我尝试构建一个应用程序,该应用程序从 iPhone 相机捕获帧并使用这些帧进行一些处理。我尝试了一些在 Internet 上找到的代码,例如这里:How to capture image without displaying preview in 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 方法的处理程序代码块,因此我永远不会从帧中获取图像。 我是否以错误的方式做某事? 感谢您的建议!

【问题讨论】:

  • 您可以检查您的AVCaptureSession 是否仍在运行。如果你提前结束它,你的 completionHandler 永远不会被调用。

标签: iphone ios avfoundation capture


【解决方案1】:

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

这意味着您有两种选择:

【讨论】:

    【解决方案2】:

    您可能需要检查NSError *error 参数。它为您提供有关成功拍摄照片的信息。 AVFoundation 错误代码是 here。我希望这有帮助。

    此外,您可能还想了解this,他解释说在同一位置有会话代码可能会导致问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2014-08-23
      相关资源
      最近更新 更多