If you want to run the camera in a custom UIView , you need to change the AVCaptureVideoPreviewLayer . you can change its borders, its position, also you can add a mask to it.
Approaching your question, the capture layer occupies the entire screen, because you have:
previewLayer?.frame = self.view.layer.frame
Change this line to this overlay frame
previewLayer?.frame = self.overLayView.layer.frame
or, if you want to place the camera layer manually using raw values:
previewLayer?.frame = CGRectMake(x,y,width,height)
Also, note that if you want to run the camera in overlay mode, you need to add a preview to this overlay view
so this line:
self.view.layer.addSublayer(previewLayer!)
will be as follows:
self.overLayView.layer.addSublayer(previewLayer!)
To stretch a layer / set a preview layer:
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) var bounds:CGRect bounds=cameraView.layer.frame; previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill; previewLayer!.bounds=bounds; previewLayer!.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); self.view.layer.addSublayer(previewLayer!)
Teja nandamuri
source share