title: Integrate iphone camera into ViewController's subview UIView
body: <p>I am using following code to embed Camera into my application view.</p>
  
  <p>Here is my code</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
      AVCaptureSession *session = [[AVCaptureSession alloc] init];
      session.sessionPreset = AVCaptureSessionPresetMedium;
  
      AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
  
      captureVideoPreviewLayer.frame = self.cameraView.bounds;
      [self.cameraView.layer addSublayer:captureVideoPreviewLayer];
  
      AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  
      NSError *error = nil;
      AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&amp;error];
      if (!input)
      {
          [Utilities alertDisplay:@"Error" message:@"Camera not found. Please use Photo Gallery instead."];
      }
  
      [session addInput:input];
  
      stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
      NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
      [stillImageOutput setOutputSettings:outputSettings];
  
      [session addOutput:stillImageOutput];
  
      [session startRunning];
  }
  
  -(AVCaptureDevice *)backFacingCameraIfAvailable{
  
      NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
      AVCaptureDevice *captureDevice = nil;
  
      for (AVCaptureDevice *device in videoDevices){
  
          if (device.position == AVCaptureDevicePositionBack){
  
              captureDevice = device;
              break;
          }
      }
  
      //  couldn't find one on the front, so just get the default video device.
      if (!captureDevice){
  
          captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
      }
  
      return captureDevice;
  }
  
  
  - (IBAction)scanButtonPressed:(id)sender
  {
      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];
  
           //self.vImage.image = image;
       }];
  }
  </code></pre>
  
  <p>The problem i am facing is, I don't get any camera opened in my cameraView and also on scanBtnPressed i get</p>
  
  <pre><code>stillImageOutput.connections = 0 objects.
  </code></pre>
  
  <p>What is wrong?</p>