Mikey Mikey - 3 months ago 27
Swift Question

How to add camera button to UIImageView

I would like to add a background image to a

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

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

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
UIImageView
, 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
UIImageView
I would like to add a background image.

Answer

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.

Comments