1

could you please help me with UIImagePickerController

I have 12 ImageViews in 1 ViewController. I need help so that each ImageView could pick different photos from Library. Someone told me to use Tags for them but I can't make it work cause i'm new in Swift.

3
  • Show what you are currently doing. Explain what issues you are having. Commented Sep 8, 2018 at 20:56
  • See stackoverflow.com/questions/44836842/… for a likely solution. Commented Sep 8, 2018 at 20:57
  • @rmaddy I'm currently doing that user will fill information about house and take photo of house's Door, Windows, Roof, Stairs and push all that information to backend. So i'm struggling with taking pics for all imageViews that won't reflect same image to all ImageVIews Commented Sep 9, 2018 at 5:37

2 Answers 2

2

Enable User Interaction Enabled for each UIImageView on a Storyboard.

enter image description here

Add TapGestureRecogniser to each UIImageView. Connect each TapGestureRecogniser with IBAction.

@IBAction func tap(_ sender: UITapGestureRecognizer) { currentImageView = sender.view as! UIImageView let picker = UIImagePickerController() picker.delegate = self self.present(picker, animated: true, completion: nil) } 

Define variable to store current UIImageView

private var currentImageView: UIImageView? = nil 

Handle image selection and assign the image to currentImageView

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { let image = info[UIImagePickerController.InfoKey.originalImage] currentImageView?.image = image as! UIImage self.dismiss(animated: true, completion: nil) } 
Sign up to request clarification or add additional context in comments.

Comments

0

It's not about the swift. You set up a "tag" number in the Interface builder (or in the code. There is a .tag property on the UIResponder descendants). After that you make a switch in your callback method like

To make it a bit clearer:

let picker: UIImagePicker! = { // setup your image picker here, don't forget to set the proper delegate }() var lastSender: AnyObject? func onTouch(_ sender: AnyObject?) { // add checks for the sender here lastSender = sender present(picker, animated: true, completion: *Your Completion*) } 

Delegate:

in

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

let image = *Get Image From Info* lastSender?.image = image

4 Comments

I'm creating everything programmatically. So I didn't understand how to setup ImagePickerController after assigning tags to ImageViews. There's thing called if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage. Should I write here which Tagged imageView i should pick?
you need to store the caller somewhere like: var lastSender: AnyObject? and then manage it. ImagePickerController (as far as I remember) is a different UIViewController, which doesn't allow an user to call another UIIImageView while picking the image. So it's kinda safe to assume that the image picked was ment for the sender. It's the fastest option which comes in my mind. in image picker callback you just set the picked image to the lastSender.image. Don't forget to add checks for the sender type though. It's easy to link a different thing to your action from the IB.
Tags are needed to get clue on the sender when you're displaying UIImage and separate the logic. If you don't need to separate something, you can simply remember the sender in your callback and set the image to it when your user is done
edited the original post to reflect what I'm talking about

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.