Search code examples
iosswift3camerauiimagepickercontrollerxib

Add object on image Picker photo display (swift3)


The code below is code for just taking a standard photo using image picker. I am working on a app that masks a photo. So What I am trying to do is block out some of the image picker with a box. So the user knows that they should not put there face in the area with a box over it. Example of image picker with box on it. For a better result when the photo is masked.

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate  {
var currentImageView: UIImageView?
var imagePicker: UIImagePickerController!
let maskView = UIImageView()

@IBOutlet var letImageVIEW: UIImageView!


@IBOutlet var finalImage: UIImageView!
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    self.dismiss(animated: true, completion: nil)
    self.currentImageView?.image = image

}
@IBAction func takePhotoLeft(_ sender: Any) {
    self.currentImageView = self.letImageVIEW
    imagePicker =  UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    present(imagePicker, animated: true, completion: nil)

    }

Solution

  • You can use UIImagePickerController property cameraOverlayView. Try this.

    @IBAction func takePhotoLeft(_ sender: Any) {
    
        self.currentImageView = self.letImageVIEW
        imagePicker =  UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .camera
    
        let blockView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150))
        blockView.backgroundColor = UIColor.red
        imagePicker.cameraOverlayView = blockView
        imagePicker.showsCameraControls = false
    
        present(imagePicker, animated: true, completion: nil)
    }