Provo a creare un'app che cattura i fotogrammi dalla fotocamera dell'iPhone e che elabora con questi frame. Ho provato alcuni pezzi di codici trovati su Internet, ad es. qui: How to capture image without displaying preview in iOSPerché AVCaptureStillImageOutput :: captureStillImageAsynchronouslyFromConnection: completionHandler non viene mai chiamato?
e ho finito con il seguente pezzo di codice:
-(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
}
Tuttavia, l'applicazione non entra nel blocco di codice del gestore del metodo captureStillImageAsynchronouslyFromConnection quindi non ho mai ottenere l'immagine dalla cornice. Sto facendo qualcosa in modo sbagliato? Grazie per i vostri suggerimenti!
È possibile verificare se il proprio 'AVCaptureSession' è ancora in esecuzione. Se lo interrompi in anticipo, il tuo completamentoHandler non viene mai chiamato. –