Ich versuche eine App zu erstellen, die Bilder von der iPhone-Kamera aufnimmt und mit diesen Bildern etwas verarbeitet. Ich habe einige Codes aus dem Internet ausprobiert, z. hier: How to capture image without displaying preview in iOSWarum AVCaptureStillImageOutput :: captureStillImageAsynchronouslyFromConnection: completionHandler wird nie aufgerufen?
und endete ich mit dem folgenden Code auf:
-(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
}
Allerdings wird die App nie in den Handler-Code-Block des captureStillImageAsynchronouslyFromConnection Verfahren so aus dem Rahmen ich das Bild nie bekommen. Mache ich etwas falsch? Danke für Ihre Vorschläge!
Sie könnten prüfen, ob Ihre AVCaptureSession noch läuft. Wenn Sie es zu früh beenden, wird Ihr CompletionHandler nie aufgerufen. –