2013-02-04 7 views
7

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!

+2

Sie könnten prüfen, ob Ihre AVCaptureSession noch läuft. Wenn Sie es zu früh beenden, wird Ihr CompletionHandler nie aufgerufen. –

Antwort

8

Ich habe festgestellt, dass der Zeiger auf AVCaptureSession nicht korrekt von AVCaptureStillImageOutput im Modus Automatisch-Referenz-Zählen erhalten bleibt.

Das heißt, Sie haben zwei Möglichkeiten:

2

Sie können den NSError * Fehlerparameter überprüfen. Sie erhalten Informationen darüber, wie das Bild erfolgreich aufgenommen wurde. AVFoundation Fehlercodes sind here. Ich hoffe das hilft.

Auch möchten Sie vielleicht über this lesen, wo er erklärt, Sitzungscode am selben Ort könnte zu Problemen führen.