Harry Rollings Harry Rollings - 27 days ago 9
Swift Question

How to find what image is displayed in UIImageView swift

I'm trying to find out what image is stored in the UIImageView using swift.

I have a UIImageView with a image in which is held in a UIScrollView. When the image is tapped, it calls a function, which I have passed the UIImageView to, but I need to be able to access the image which is held inside the UIImageView.

This is how I've defined the image

let dogImage = UIImage(named: "dog")


And I'd like to get back to the that constant in the called function, but I can only figure out how to get the image view and can't access the image that is inside of it (which would be dogImage)

func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
//this function gets called when an image is tapped

//tappedImageView is the image view that was tapped.
let tappedImageView = gestureRecognizer.view!


How would I do this? Thanks

Answer

You can get to the UIImage object like so:

func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
  if let tappedImageView = gestureRecognizer.view as? UIImageView {
    print(tappedImageView.image)
  }
}