Swift, How to get the currently displayed image file name from UIImageView

I want to get the image file name that is currently displayed in UIImageView. I tried to do it as follows:

let currentImage = alien.image // !alien is my image view println(currentImage?.description) 

but he prints:

 Optional("<UIImage: 0x7fa61944c3d0>") 
+6
source share
2 answers

You cannot do this. Neither in fast, nor in objective-c.

You need to do to save the data you want to receive. This ... save the name somewhere and use it to upload the image. Not the other way around.

So, create a property something like imageName , and then use it to load the image.

+9
source

As a job, for images that I need to reference later, I use the recovery identifier to save the image name.

I used the recovery identifier so that I could connect several buttons to the same @IBAction and identify them based on the name of the image stored in the recovery identifier and trigger the logic of what I want to show or hide.

There may be better ways, but it worked.

I entered the image name as the recovery identifier.

Here I specify the file for the image.

enter image description here

And I just copied this and put it as a recovery identifier.

(note: this does not mean that it was intended to be used, since it is really intended to set up a link to the state, but if this does not apply to the purpose of your submission, then it should work fine.)

enter image description here

Link to the code when selecting a button.

 //Connected to several onboarding buttons. @IBAction func onBoardingButton(sender: UIButton) { println(sender.restorationIdentifier) } 

Printed RID.

enter image description here


You can also tag your images and save a link to these images using a tag.

enter image description here

And the link is just as simple.

 @IBAction func onBoardingButton(sender: UIButton) { println(sender.restorationIdentifier!) println(sender.tag) } 

It does not seem so far that we can distinguish which file was used to fill the image (which I also know based on a little glance) by attaching hard links to the view (image, button, etc.). allows me to make a connection code code and find out which image (or in my button) is used.

+3
source

Source: https://habr.com/ru/post/982487/


All Articles