我嘗試構建一個應用程序,它捕獲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永遠不會被調用。 –