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

Jak*_*kub 7 iphone capture avfoundation ios

我尝试构建一个从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       
         }
Run Code Online (Sandbox Code Playgroud)

但是,应用程序永远不会进入captureStillImageAsynchronouslyFromConnection方法的处理程序代码块,因此我永远不会从框架中获取图像.我是以错误的方式做某事吗?谢谢你的建议!

tha*_*tha 8

我发现在自动参考计数模式下,指针AVCaptureSession无法正确保留AVCaptureStillImageOutput.

这意味着您有两种选择:

  • 禁用自动引用计数,或

  • 保留指向AVCaptureSession自己的指针(例如,通过将其指定给控制器中的强属性).