title: AVFoundation camera preview layer not working
body: <p>So, I am trying to implement a camera using AVFoundation. I think I do everything right. this is what i am doing</p>
  
  <ol>
  <li>create session</li>
  <li>get devices of video type</li>
  <li>loop through devices to get the camera at the back</li>
  <li>get a device input using the device mentioned in #3 and add it to the session</li>
  <li>create an output of type <code>AVCaptureStillImageOutput</code></li>
  <li>set output settings and add it to the session</li>
  <li>get a CALayer from my view 2(will explain below what I mean by view 2)</li>
  <li>create an instance of <code>AVCaptureVideoPreviewLayer</code></li>
  <li>add it to the layer mentioned in #7</li>
  <li>start running the session</li>
  </ol>
  
  <p>So I have 2 views one over the other. The one on top is View 1 and the one below is view 2. View 1 is supposed to provide with custom camera controls.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>self.session = [[AVCaptureSession alloc]init];
  [self.session setSessionPreset:AVCaptureSessionPresetHigh];
  NSArray *devices = [[NSArray alloc]init];
  devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  for (AVCaptureDevice *device in devices){
      if([device position] == AVCaptureDevicePositionBack){
          self.device = device;
          break;
      }
  }
  NSError *error;
  self.input = [[AVCaptureDeviceInput alloc]initWithDevice:self.device error:&amp;error];
  if([self.session canAddInput:self.input]){
      [self.session addInput:self.input];    
  }
  
  
  self.stillImageOutput = [[AVCaptureStillImageOutput alloc]init];
  NSDictionary *outputSettings = @{AVVideoCodecKey : AVVideoCodecJPEG};
  [self.stillImageOutput setOutputSettings:outputSettings];
  
  [self.session addOutput:self.stillImageOutput];
  
  CALayer *cameraLayer = self.cameraView.layer;
  self.cameraView.backgroundColor = [UIColor clearColor];
  
  AVCaptureVideoPreviewLayer *preview = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
  [cameraLayer addSublayer:preview];
  
  [self.session startRunning];
  </code></pre>
  
  <p>What I get is View 1(it has a .png image as its background. the image has a hole so that the view under it, view 2 can be visible) and view 2 is visible but I dont see what I am supposed to. Because I changed the background color for view 2 to clear color I see all black. I am supposed to see what the camera sees.</p>