IOS Swift - Custom camera overlay

后端 未结 1 1666
无人及你
无人及你 2020-12-23 23:10

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

相关标签:
1条回答
  • 2020-12-23 23:50

    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!)
    
    0 讨论(0)
提交回复
热议问题