title: Change location of UIView within captureOutput
body: <p>Currently I have an AVCaptureSession running in my Swift app and I have a UIView with a CGRect that I want to move around over the camera feed. I have been trying to update the location of the UIView's frame by changing the coordinates in the captureOutput function but as I change them they are not updating. What is the proper way to change the location of UIView so that it updates on the fly?</p>
  
  <p>Here's the section of code where the capture session is made:</p>
  
  <pre><code>class CameraViewController: UIViewController, UIImagePickerControllerDelegate, AVCaptureVideoDataOutputSampleBufferDelegate
  </code></pre>
  
  <p>{</p>
  
  <pre><code>@IBOutlet weak var cameraView: UIImageView!
  var rectangleView : UIView!
  var captureSession : AVCaptureSession?
  var previewLayer : AVCaptureVideoPreviewLayer?
  var rect = CGRect(x: 150, y: 150, width: 50, height: 50)
  var xCoord: Float = 0.0
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      //---editing rectangle view---
      rectangleView = UIView()
      rectangleView.layer.borderColor = UIColor(hue: 0.2444, saturation: 1, brightness: 0.96, alpha: 1).cgColor
      rectangleView.layer.borderWidth = 50;
      rectangleView.tag = 1
      self.view.addSubview(rectangleView)
      //----------------------------
  
  
      captureSession = AVCaptureSession()
      captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
  
      let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
  
      var input = AVCaptureDeviceInput()
      do{
          input = try AVCaptureDeviceInput(device: backCamera)
      }catch{
          print("Error finding back Camera")
      }
      var error : NSError?
  
      if (error == nil &amp;&amp; (captureSession?.canAddInput(input))!)
      {
          captureSession?.addInput(input)
  
          let dataOutput = AVCaptureVideoDataOutput()
          dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "string buffer delegate"))
  
          if (captureSession?.canAddOutput(dataOutput))!
          {
  
              captureSession?.addOutput(dataOutput)
              previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
              previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
              previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
              //                cameraView.layer.addSublayer(previewLayer!)
              self.view.layer.addSublayer(previewLayer!)
              self.view.bringSubview(toFront: rectangleView)
              previewLayer?.frame = self.view.bounds
              rectangleView.frame = rect
              captureSession?.startRunning()
  
          }
      }
  }
  </code></pre>
  
  <p>Here's the captureOutput function:
  the setY method is an extension to the UIView class that changes the y coordinate of the frame.</p>
  
  <pre><code>func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
  
      let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer!)
      let x = rectangleView.frame.origin.x
      let y = rectangleView.frame.origin.y - 1
      let height = rectangleView.frame.size.height
      let width = rectangleView.frame.size.width
  
      rectangleView.setY(y: y)
  
      print(rectangleView.frame.origin.y)
  
      //*** Making UIImage out of pixelBuffer ****
      let ciImage = CIImage(cvPixelBuffer: pixelBuffer!)
      let pixelBufferWidth = CGFloat(CVPixelBufferGetWidth(pixelBuffer!))
      let pixelBufferHeight = CGFloat(CVPixelBufferGetHeight(pixelBuffer!))
      let imageRect:CGRect = CGRect(x: 0,y: 0,width: pixelBufferWidth, height: pixelBufferHeight)
      let ciContext = CIContext.init()
      let cgimage = ciContext.createCGImage(ciImage, from: imageRect )
      let image = UIImage(cgImage: cgimage!)
      //******************************************
  
  
  
  }
  </code></pre>
  
  <p>Thanks for the help,</p>
  
  <p>-nfarrell</p>