0
votes

I have 2 UIImageView, named "artistImage" and "albumImage", each one contains 1 tap gesture and all the gestures are connected to 1 @IBAction, named "artistImageTap". Those tap gestures are drag from object library and place over my ImageView.

Storyboard - Code enter image description here

My app have a list of artists, albums and songs, when a song is tapped, the app moves to this view and display its details. If I click the add button, the app moves to this view but this time all the text fields are editable, the images are default and user can tap them to select image from library to create new song.

My problem is I don't know how to identify which UIImageView is tapped. As you can see in the picture, I tried picker.restorationIdentifier but it always returns nil.

@IBAction func artistImageTap(_ sender: UITapGestureRecognizer) {
    let imagePickerController = UIImagePickerController()

    // Only allow photos to be picked, not taken.
    imagePickerController.sourceType = .photoLibrary
    // Make sure ViewController is notified when the user picks an image.
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    // The info dictionary may contain multiple representations of the image. You want to use the original.
    guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display the selected image.
    if picker.restorationIdentifier == "artistImage" {
        artistImage.image = selectedImage
    } else {
        albumImage.image = selectedImage
    }
    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

Every help is appreciated!!

2
Nobody even knows how you are creating a gesture recognizer.El Tomato
use sender.view and setup a tag in your UIImageView, after that you can use sender.view.tag as identifierReinier Melian
@ElTomato I added them by dragging from object library. Sorry for not mention that!hell2809
@ReinierMelian I tried to setup tag for my view, scrollview, content view, even the ImageView, but none of them showed up when I used sender.view.taghell2809
use debugPrint(sender.view) and let me know what prints @hell2809Reinier Melian

2 Answers

1
votes
  • Add tag to both UIImageView from storyboard. like artistImage = 1001 and albumImage = 1002

    @IBAction func artistImageTap(_ sender: UITapGestureRecognizer) {
    
    if sender.view?.tag == 1001 {
    
        selectedTag = 1001
    
    } else if sender.view?.tag == 1002 {
    
       selectedTag = 1002
    
    }
    
    let imagePickerController = UIImagePickerController()
    
    // Only allow photos to be picked, not taken.
    imagePickerController.sourceType = .photoLibrary
    // Make sure ViewController is notified when the user picks an image.
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, completion: nil)
    } 
    
  • store selected tag in one variable.

  • now you can check which on image user have tapped with selectedTag variable

0
votes

First of all set tags for tap gestures when you are applying it on images.

tapGestureArtistImage.tag = 0;
tapGestureAlbumImage.tag = 1;

then send tap gesture as parameters in artistImageTap method

- (void) artistImageTap:(UITapGestureRecognizer*)sender
{
  if(sender.tag == 0)
  {
    // artistImage tapped
  }
  else if (sender.tag == 1)
  {
    // albumImage tapped
  }
}