Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift, How to get currently displayed image file name from UIImageView [duplicate]

Tags:

ios

swift

I want to get the image file name which is currently displayed at UIImageView. I tried to get it as follow:

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

but it prints:

Optional("<UIImage: 0x7fa61944c3d0>")
like image 292
semirturgay Avatar asked Feb 14 '15 12:02

semirturgay


Video Answer


2 Answers

You can't do this. Neither in swift nor objective-c.

The thing to do is to store the data you want to retrieve. That is... store the name somewhere and use that to load the image. Not the other way around.

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

like image 141
Fogmeister Avatar answered Sep 29 '22 14:09

Fogmeister


As a work around, for images that I need to reference at a later time, I use the restoration ID to store the image name.

I used restoration ID in this way so that I could connect multiple buttons to the same @IBAction and identify them based on the image name stored in the restoration ID and run logic about what I want to display or hide.

There might be better ways but this worked in a pinch.

I put the image name in as the restoration ID.

Here is where I designate the file for the image..

enter image description here

And I just copy that and put it in as the restoration ID.

(note: that is not what this was intended to be used for as it is really meant for customizing state reference but if that is not relevant to the purpose of your view, then it should work fine.)

enter image description here

Referenced in code when the button is selected.

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

    println(sender.restorationIdentifier)



}

RID printed out.

enter image description here


You can also tag your images and keep the reference to those images via the tag.

enter image description here

And the reference is just as easy.

@IBAction func onBoardingButton(sender: UIButton) {

    println(sender.restorationIdentifier!)
    println(sender.tag)


}

While it doesn't seem like we can discern what file was used to fill the imageview (that I know of and based on a little looking around myself) attaching hard references to a view (image, button, etc..) allows me to make the connection code side and figure out which image (or in my case button) is being used.

like image 43
Christopher Wade Cantley Avatar answered Oct 01 '22 14:10

Christopher Wade Cantley