hello I would like to open a camera in my app like this
I want to open a camera only in the middle of the section so user can take a snap only in the recta
If you want to start camera in a custom UIView
, you need to change the AVCaptureVideoPreviewLayer
. you can change its bounds, its position, also you can add mask to it.
Coming to your question, the capture layer is taking full screen because you have:
previewLayer?.frame = self.view.layer.frame
Change this line to that overlay frame
previewLayer?.frame = self.overLayView.layer.frame
or, if you want to position the camera layer manually using raw values:
previewLayer?.frame = CGRectMake(x,y,width,height)
Also , note that, if you want to start the camera in overlay view, you need to add the subview to that overlay view
so this line:
self.view.layer.addSublayer(previewLayer!)
will be this:
self.overLayView.layer.addSublayer(previewLayer!)
To stretch the layer/ fit the 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!)