2013-04-09 40 views

回答

1

以下是完整的解決方案。不要忘記導入適當的框架和頭文件。

#import <AVFoundation/AVFoundation.h> 
#import <ImageIO/CGImageProperties.h> 

AVCaptureStillImageOutput *stillImageOutput; 
AVCaptureSession *session;  

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     [self setupCaptureSession]; 
     // Do any additional setup after loading the view, typically from a nib.  
    } 

    -(void)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; } 
     } 

     [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection 
     completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *__strong error) { 
      CFDictionaryRef exifAttachments = CMGetAttachment(imageDataSampleBuffer, kCGImagePropertyExifDictionary, NULL); 
      if (exifAttachments) 
      { 
       // Do something with the attachments. 
       NSLog(@"attachements: %@", exifAttachments); 
      } 
      else 
       NSLog(@"no attachments"); 

      NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
      UIImage *image = [[UIImage alloc] initWithData:imageData]; 
      }]; 

    } 


    // Create and configure a capture session and start it running 
    - (void)setupCaptureSession 
    { 
     NSError *error = nil; 

     // Create the session 
     session = [[AVCaptureSession alloc] init]; 

     // Configure the session to produce lower resolution video frames, if your 
     // processing algorithm can cope. We'll specify medium quality for the 
     // chosen device. 
     session.sessionPreset = AVCaptureSessionPreset352x288; 

     // Find a suitable AVCaptureDevice 
     AVCaptureDevice *device = [AVCaptureDevice 
            defaultDeviceWithMediaType:AVMediaTypeVideo]; 
     [device lockForConfiguration:nil]; 

     device.whiteBalanceMode = AVCaptureWhiteBalanceModeLocked; 
     device.focusMode = AVCaptureFocusModeLocked; 
     [device unlockForConfiguration]; 

     // Create a device input with the device and add it to the session. 
     AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device 
                      error:&error]; 
     if (!input) { 
      // Handling the error appropriately. 
     } 
     [session addInput:input]; 




     stillImageOutput = [AVCaptureStillImageOutput new]; 
     NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; 
     [stillImageOutput setOutputSettings:outputSettings]; 
     if ([session canAddOutput:stillImageOutput]) 
      [session addOutput:stillImageOutput]; 


     // Start the session running to start the flow of data 
     [session startRunning]; 
     [self captureNow]; 

    }