iosswiftuiimagepickercontrollercameraoverlayview

How to constraint overlay to UIImagePickerController camera preview only


I am using UIImagePickerController() to display camera in my swift iOS app and let user take pictures. I would like to show a grid as overlay so I started implementing one as .cameraOverlayView

The problem is that the overlay image or views will spread over the black bars on top and bottom of the camera view. This is undesirable as the grid line will run over the camera button, cancel button etc.

I could not find a method to access the frame of the camera preview window to constraint the overlay just to that. What am I missing?


Solution

  • Actually, this may be a little annoy, there is no way to get the sizes of those bars or the camera preview area, and you can't achieve the subviews of UIImagePickerController and so you can't add constraints for that. But, you can use the fact that the preview camera area aspect ratio is known as 4/3 always and the overlay width should be the width of the screen and the height should be the (widthScreen / 3) * 4

    another way is to set imagePicker.showsCameraControls = false and build custom controls with the overlay on fullScreen.
    (use the imagePicker.cameraViewTransform in order to set the camera preview area frame full screen).