0

I have programmatically defined a UIImageView as such:

  let imageConstant: UIImageView = {
    let image = UIImageView()
    image.clipsToBounds = true
    image.contentMode = .scaleAspectFit
    image.translatesAutoresizingMaskIntoConstraints = false
     image.image = UIImage(named: "imageNameSavedInAssets")
    return image
}()

How do I access the name of imageConstant's image? That is, how can I access the name of imageConstant.image which reads "imageNameSavedInAssets"?

  • Why do you need it? This sounds like an XY problem. You should check your model, not your views. – Sweeper Sep 04 '19 at 17:19
  • 2
    This is just a homework assignment that I need to complete. Been reading up documentation and properties but I do not know how this is done. –  Sep 04 '19 at 17:23
  • I doubt the homework asked you to do _this specific task_ of getting the name of the image given this exact code. What did the homework ask you to do? – Sweeper Sep 04 '19 at 17:24
  • 2
    Nope, the code is mine, I am supposed to interpolate the name of the said image into a statement such that it reads: "You have chosen a \(nameOfTheImage)" which will actually read "You have chosen a banana" –  Sep 04 '19 at 17:27
  • 1
    You can achieve that in other ways. But I think there's no way to get the name of any UIImage, it's only used to get the file but not stored. – emelagumat Sep 04 '19 at 18:01
  • Does this answer your question? [UIImageView - How to get the file name of the image assigned?](https://stackoverflow.com/questions/1740274/uiimageview-how-to-get-the-file-name-of-the-image-assigned) – amadour Jun 08 '22 at 14:52

3 Answers3

1

You can write your custom class to set and get the image name.

class customImageView: UIImageView {

var imageName: String?

func setImage(_ name: String) {
    imageName = name
    image = UIImage(named: imageName ?? "")!
}

}

accessibilityIdentifier is usually used in user accessibility(not suitable everywhere).

The most recommended way is to use Tag property of view. Depending on your tag change the image.

var imageView = UIImageView(image: UIImage(named: "image1"))
imageView.tag = 1
switch imageView.tag {
case 1:
imageView.image = UIImage(named: "image1")
imageView.tag = 2
case 2:
imageView.image = UIImage(named: "image2")
imageView.tag = 3
default:
imageView.image = UIImage(named: "image3")
imageView.tag = 1
}

for more info, refer the thread : swift, How to get currently displayed image file name from UIImageView

Amyth
  • 383
  • 3
  • 9
0

Subclass UIImageView and add a custom variable that will hold the name of the image file. Something like,

class customImageView: UIImageView {

var fileName: String?

  init(name: String?) {
    super.init()
    fileName = name
  }
}

Add whatever other initializers XCode requires.

let imageView = customImageView("banana")

print(imageView.fileName)
cora
  • 1,916
  • 1
  • 10
  • 18
0

accessibilityIdentifier property of UIImageView correctly returns the input string you gave in its image instantiation, or nil if the string is not a valid name.