我尝试构建一个应用程序,它捕获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方法的处理程序代码块,所以我从来没有从帧图像。 我在做错事吗? 感谢您的建议!
您可能会检查'AVCaptureSession'是否仍在运行。如果你尽早结束它,你的completionHandler永远不会被调用。 –