我尝试制作一个应用程序,该应用程序可以捕获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 方法的处理程序代码块,因此我从帧中永远无法获取图像。 我是否做错了什么? 感谢您的建议!
AVCaptureSession
是否仍在运行。如果您过早地结束它,您的 completionHandler 将永远不会被调用。 - Boris Prohaska