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.

  • Show what you are currently doing. Explain what issues you are having. – rmaddy Sep 08 '18 at 20:56
  • See https://stackoverflow.com/questions/44836842/why-does-my-uiimageview-replace-the-second-one for a likely solution. – rmaddy Sep 08 '18 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 – Акарыс Турганбекулы Sep 09 '18 at 05:37

2 Answers2

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)
}
Daniil Subbotin
  • 6,138
  • 5
  • 20
  • 24
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

D.3
  • 145
  • 7
  • 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? – Акарыс Турганбекулы Sep 08 '18 at 20:40
  • 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. – D.3 Sep 08 '18 at 21:07
  • 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 – D.3 Sep 08 '18 at 21:11
  • edited the original post to reflect what I'm talking about – D.3 Sep 08 '18 at 21:29