【发布时间】: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