2013-02-04 37 views
7

मैं एक ऐप बनाने की कोशिश करता हूं जो आईफोन कैमरे से फ्रेम को कैप्चर करता है और इन फ्रेमों के साथ कुछ प्रोसेसिंग करता है। मैंने इंटरनेट पर पाए गए कोड के कुछ टुकड़े करने की कोशिश की, उदा। यहाँ: How to capture image without displaying preview in iOSक्यों AVCaptureStillImageOutput :: captureStillImageAsynchronouslyFromConnection: completHandler कभी नहीं कहा जाता है?

और मैं कोड का निम्न भाग के साथ समाप्त हो गया:

-(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 विधि का हैंडलर कोड ब्लॉक में हो जाता है तो मैं कभी नहीं फ्रेम से छवि मिलता है। क्या मैं गलत तरीके से कुछ कर रहा हूं? आपके सुझावों के लिए धन्यवाद!

+2

आप जांच सकते हैं कि आपका 'AVCaptureSession' अभी भी चल रहा है या नहीं। यदि आप इसे जल्दी खत्म करते हैं, तो आपका पूरा होने वाला हैंडलर कभी नहीं बुलाया जाता है। –

उत्तर

8

मुझे पता चला है कि AVCaptureSession पर सूचक स्वचालित-संदर्भ-गणना मोड में AVCaptureStillImageOutput द्वारा ठीक से बनाए रखा नहीं जाता है।

इसका मतलब है आप दो विकल्प हैं:

  • disable automatic reference counting, या

  • सूचक AVCaptureSession लिए (उदाहरण के लिए अपने नियंत्रक में एक मजबूत संपत्ति को यह बताए द्वारा) अपने आप को बनाए रखने के लिए।

2

आप NSError * त्रुटि पैरामीटर को देखना चाहेंगे। यह आपको सफलतापूर्वक ली गई तस्वीर के बारे में जानकारी देता है। AVFoundation त्रुटि कोड here हैं। आशा है कि ये आपकी मदद करेगा।

इसके अलावा, आप के बारे में this, जहां उन्होंने समस्याओं को जन्म दे सकता है एक ही स्थान पर सत्र कोड होने बताता भी पढ़ सकते हैं।