title: How to set the camera size on a preview view layer
body: <p>I use below code to show a camera on a iPhone device. The camera can be displayed the UI and I am able to take picture. However, the camera is only shown in the middle of the previewLayer. How to make the camera to fill up all space on a AVCaptureVideoPreviewLayer? I have tried to use AVLayerVideoGravityResizeAspectFill gravity but it will make the image out of shape.</p>
  
  <pre><code>self.session = AVCaptureSession()
          do {
              let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
              try self.videoInput = AVCaptureDeviceInput(device: device)
          } catch {
  
          }
          if ((self.session?.canSetSessionPreset(AVCaptureSessionPresetHigh)) != nil){
              self.session?.sessionPreset = AVCaptureSessionPresetHigh
          }
          self.stillImageOutput = AVCaptureStillImageOutput()
  
          self.stillImageOutput?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
          if self.session!.canAddInput(self.videoInput){
              self.session?.addInput(self.videoInput)
          }
          if self.session!.canAddOutput(self.stillImageOutput){
              self.session?.addOutput(self.stillImageOutput)
          }
  
  self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
          //self.backView.frame = CGRectMake(0, 0, self.view.bounds.width,self.view.bounds.width)
          let viewLayer:CALayer = self.backView.layer
          viewLayer.masksToBounds = true
          let bounds:CGRect = viewLayer.bounds
          self.previewLayer?.frame = bounds
          self.previewLayer?.backgroundColor = UIColor.blackColor().CGColor
          self.previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect//AVLayerVideoGravityResizeAspect
          self.backView.layer.addSublayer(self.previewLayer!)
  </code></pre>
  
  <p>Below is the screenshot of the camera.</p>
  
  <p><a href="https://i.stack.imgur.com/Z3XGJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z3XGJ.jpg" alt="enter image description here"></a></p>