3

My code right here displays a red line on the top of the imagePicker when taking a photo. I want to do the same thing but add a image in the middle of the red line. See pic. I added the word image in a yellow box as a example of what I am trying to code.

    @IBAction func takePhoto(_ sender: UIButton) {
    imagePicker =  UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    present(imagePicker, animated: true, completion: nil)
    let mainView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height-150))
    let blockView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150))
    blockView.backgroundColor = UIColor.red
    mainView.addSubview(blockView)
    imagePicker.cameraOverlayView = mainView
}

enter image description here

1 Answers1

2

Just do:

let frame             = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150)
let blockView         = UIImageView.init(frame: frame)
blockView.contentMode = .scaleAspectFit
blockView.image       = UIImage(named: "NameOfYourImage")  // Just an example.

See UIImageView.

The frame above needs adjustment of course, to allow for top and bottom margins you show in your screenshot.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • How would I make the photo in the center. No matter if the device is in portrait and landscape mode. Right now it always stays in the center for landscape mode but not for portrait. Thanks. –  Dec 08 '17 at 17:23
  • That's a whole other subject, but you should use auto-layout (or autoresizing masks). There are plenty tutorials. – meaning-matters Dec 08 '17 at 18:07