title: how to crop a AVCaptureVideoPreviewLayer in a shape of square in Swift?
body: <p>I am using AVCaptureVideoPreviewLayer to make a camera application. For some reason, I need to use just a part of the screen capturing in a square shape to show a display output. It is something similar to the early question: <a href="https://stackoverflow.com/questions/30014651/cropping-avcapturevideopreviewlayer-output-to-a-square">Cropping AVCaptureVideoPreviewLayer output to a square</a></p>
  
  <p>However, I don't need to make the image taken to be a square shape. The only thing that I want is having a rectangle just show the part of the red area from the camera. I attached an example picture below.</p>
  
  <p><a href="https://i.stack.imgur.com/irPXr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/irPXr.png" alt="enter image description here"></a></p>
  
  <p>Anyone can provide some example code in swift to show some hints how can I do that?</p>
  
  <p>[updated] The code that I am using:
  * The code can also be downloaded from my bitbucket(<a href="https://bitbucket.org/fireares/swift-avcam_swiftwithcropfunction/src/7ca5aef02173e941e255623e230129cc2e304a7a/AVCamSwift-master-3?at=questionStackoverflow" rel="nofollow noreferrer">https://bitbucket.org/fireares/swift-avcam_swiftwithcropfunction/src/7ca5aef02173e941e255623e230129cc2e304a7a/AVCamSwift-master-3?at=questionStackoverflow</a>)</p>
  
  <pre><code>    override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
      addTargetLayer()
      var session: AVCaptureSession = AVCaptureSession()
      self.session = session
  
      self.previewView.session = session
  
      self.checkDeviceAuthorizationStatus()
  
      var sessionQueue: dispatch_queue_t = dispatch_queue_create("session queue",DISPATCH_QUEUE_SERIAL)
  
      self.sessionQueue = sessionQueue
      dispatch_async(sessionQueue, {
          self.backgroundRecordId = UIBackgroundTaskInvalid
  
          var videoDevice: AVCaptureDevice! = ViewController.deviceWithMediaType(AVMediaTypeVideo, preferringPosition: AVCaptureDevicePosition.Back)
          var error: NSError? = nil
  
          var videoDeviceInput: AVCaptureDeviceInput? =  AVCaptureDeviceInput(device: videoDevice, error: &amp;error)
  
          if (error != nil) {
              println(error)
  
          }
  
          if session.canAddInput(videoDeviceInput){
              session.addInput(videoDeviceInput)
              self.videoDeviceInput = videoDeviceInput
  
              dispatch_async(dispatch_get_main_queue(), {
                  // Why are we dispatching this to the main queue?
                  // Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread.
                  // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation.
  
                  var orientation: AVCaptureVideoOrientation =  AVCaptureVideoOrientation(rawValue: self.interfaceOrientation.rawValue)!
                  (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = orientation
  
              })
  
          }
  
                      if error != nil{
              println(error)
          }
  
          var movieFileOutput: AVCaptureMovieFileOutput = AVCaptureMovieFileOutput()
          if session.canAddOutput(movieFileOutput){
              session.addOutput(movieFileOutput)
  
  
              var connection: AVCaptureConnection? = movieFileOutput.connectionWithMediaType(AVMediaTypeVideo)
              let stab = connection?.supportsVideoStabilization
              if (stab != nil) {
                  connection!.enablesVideoStabilizationWhenAvailable = true
              }
  
          //   self.movieFileOutput = movieFileOutput
  
          }
  
          var stillImageOutput: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
          if session.canAddOutput(stillImageOutput){
              stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
              session.addOutput(stillImageOutput)
  
              self.stillImageOutput = stillImageOutput
          }
  
  
      })
  
  
  }
  override func viewWillAppear(animated: Bool) {
      dispatch_async(self.sessionQueue!, {
          self.addObserver(self, forKeyPath: "stillImageOutput.capturingStillImage", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: &amp;CapturingStillImageContext)
  
  
          NSNotificationCenter.defaultCenter().addObserver(self, selector: "subjectAreaDidChange:", name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: self.videoDeviceInput?.device)
  
  
          weak var weakSelf = self
  
          self.runtimeErrorHandlingObserver = NSNotificationCenter.defaultCenter().addObserverForName(AVCaptureSessionRuntimeErrorNotification, object: self.session, queue: nil, usingBlock: {
              (note: NSNotification?) in
              var strongSelf: ViewController = weakSelf!
              dispatch_async(strongSelf.sessionQueue!, {
              if let sess = strongSelf.session{
                      sess.startRunning()
                  }
  
              })
  
          })
  
          self.session?.startRunning()
  
      })
  }
  </code></pre>