Mikey Mikey - 1 year ago 147
Swift Question

How to add camera button to UIImageView

I would like to add a background image to a

. At the moment, in the empty frame of the
I have a
to open the camera.

let imageView = whiskyPicture
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(DataInsertController.imageTapped(_:)))
imageView!.userInteractionEnabled = true

func imageTapped(sender: UITapGestureRecognizer) {
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.sourceType = .Camera

presentViewController(imagePicker, animated: true, completion: nil)

Now I would like to add a background image in the
, that the user can see where to tap. The best thing would be to add the camera icon of iOS to the UIImage. After picking a photo, the background image should be gone.

Any advice how to do that?

Storyboard looks like this:

DataInsertViewController Storyboard

In the
I would like to add a background image.

Answer Source

In your imageViewjust set the camera image directly and set the Mode property to Center. Just like this image

enter image description here

Now in the didFinishPickingMediaWithInfo method just change the contentMode of imageView to what you want like Scale To Fill, Aspect Fit, Aspect Fill, etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download