2011-03-08 9 views
22

क्या ऐप्पल नियंत्रण के बिना आईफोन पर कोड में एक तस्वीर लेने का कोई तरीका है? मैंने ऐप का एक गुच्छा देखा है जो ऐसा करता है, लेकिन मुझे यकीन नहीं है कि कौन सी एपीआई कॉल का उपयोग करना है।नियंत्रण के बिना आईफोन पर एक तस्वीर लें

+0

"ऐप्पल कंट्रोल" द्वारा, आपको लगता है कि UIImagePickerController का उपयोग किए बिना आपका मतलब है। – aceofspades

उत्तर

24

हां, ऐसा करने के दो तरीके हैं। एक, आईओएस 3.0+ में उपलब्ध UIImagePickerController वर्ग का उपयोग करना है, showsCameraControls संपत्ति को NO पर सेट करना, और cameraOverlayView संपत्ति को अपने स्वयं के कस्टम नियंत्रण में सेट करना है। दो, आईओएस 4.0+ में उपलब्ध है AVCaptureSession को कॉन्फ़िगर करना, इसे उचित कैमरा डिवाइस का उपयोग करके AVCaptureDeviceInput प्रदान करना, और AVCaptureStillImageOutput। पहला दृष्टिकोण बहुत आसान है, और अधिक आईओएस संस्करण पर काम करता है, लेकिन दूसरा दृष्टिकोण आपको फोटो रिज़ॉल्यूशन और फ़ाइल विकल्पों पर अधिक नियंत्रण देता है।

35

संपादित करें: जैसा कि नीचे दी गई टिप्पणियों में सुझाया गया है, मैंने अब स्पष्ट रूप से दिखाया है कि AVCaptureSession को घोषित करने और आरंभ करने की आवश्यकता है। ऐसा लगता है कि कुछ शुरुआती गलत कर रहे थे या AVCaptureSession को एक विधि में स्थानीय चर के रूप में घोषित कर रहे थे। यह काम नहीं करेगा।

कोड के बाद

उपयोगकर्ता इनपुट के बिना एक तस्वीर AVCaptureSession का उपयोग कर लेने के लिए अनुमति देता है:

// Get all cameras in the application and find the frontal camera. 
AVCaptureDevice *frontalCamera; 
NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 

// Find the frontal camera. 
for (int i = 0; i < allCameras.count; i++) { 
    AVCaptureDevice *camera = [allCameras objectAtIndex:i]; 

    if (camera.position == AVCaptureDevicePositionFront) { 
     frontalCamera = camera; 
    } 
} 

// If we did not find the camera then do not take picture. 
if (frontalCamera != nil) { 
    // Start the process of getting a picture. 
    session = [[AVCaptureSession alloc] init]; 

    // Setup instance of input with frontal camera and add to session. 
    NSError *error; 
    AVCaptureDeviceInput *input = 
    [AVCaptureDeviceInput deviceInputWithDevice:frontalCamera error:&error]; 

    if (!error && [session canAddInput:input]) { 
     // Add frontal camera to this session. 
     [session addInput:input]; 

     // We need to capture still image. 
     AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init]; 

     // Captured image. settings. 
     [output setOutputSettings: 
     [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]]; 

     if ([session canAddOutput:output]) { 
      [session addOutput:output]; 

      AVCaptureConnection *videoConnection = nil; 
      for (AVCaptureConnection *connection in output.connections) { 
       for (AVCaptureInputPort *port in [connection inputPorts]) { 
        if ([[port mediaType] isEqual:AVMediaTypeVideo]) { 
         videoConnection = connection; 
         break; 
        } 
       } 
       if (videoConnection) { break; } 
      } 

      // Finally take the picture 
      if (videoConnection) { 
       [session startRunning]; 

       [output captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 

        if (imageDataSampleBuffer != NULL) { 
         NSData *imageData = [AVCaptureStillImageOutput 
            jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
         UIImage *photo = [[UIImage alloc] initWithData:imageData];                 
        } 

       }]; 
      } 
     } 
    } 
} 

सत्र चर प्रकार AVCaptureSession की है और वर्ग की ज फ़ाइल में घोषित किया गया है (या तो एक संपत्ति के रूप में या एक निजी के रूप में वर्ग के सदस्य):

AVCaptureSession *session; 

यह तो कक्षा में उदाहरण के लिए कहीं न कहीं से प्रारंभ करने की आवश्यकता होगी 'init विधि:

session = [[AVCaptureSession alloc] init] 
+1

बहुत उपयोगी उदाहरण, धन्यवाद। – aceofspades

+4

अब इस कोड को आजमा रहे हैं, यह सब तब तक काम करता है जब तक '[आउटपुट कैप्चरस्टिल इमेजएसिंक्रोनिसफ्रॉम कनेक्शन: ...' लेकिन फिर बंद हो जाता है, जिसके परिणामस्वरूप फोटो वास्तव में कभी नहीं लिया जा रहा है। मैंने तुरंत इसके बाद लाइन पर लॉगिंग जोड़ा और यह कभी भी लॉग नहीं हुआ। कोड आपके द्वारा लिखे गए शब्द को क्रियान्वित करता है और मैंने उस बिंदु तक कोड की पुष्टि की है (एक बटन जोड़ा और इसे .xib में लगाया, ठीक से आग लगती है)। मैंने AVFoundation.framework और AssetsLibrary.framework भी शामिल किया है। कोई विचार? –

+0

क्या आपने इस लाइन पर ब्रेकपॉइंट डाला था: अगर (imageDataSampleBuffer! = NULL) क्या यह मारा जाता है? अगर यह हिट करता है, तो त्रुटि शून्य नहीं है? –

संबंधित मुद्दे