17

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>")
semirturgay
  • 4,151
  • 3
  • 30
  • 50

2 Answers2

22

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.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • which variable can we use so the image has a unique name. At the moment i am testing with storing a static image name ("tempImage") and the image is overwritten every time. But if we can't get the image name, what can i use for image name code now: `saveImage(selectedImage, path: fileInDocumentsDirectory("tempImage"))` – alex Oct 22 '15 at 18:10
  • ok i use ` // function for images func uniqueFilename() ->String{ let uuid = NSUUID().UUIDString print(uuid) return uuid }` – alex Oct 22 '15 at 18:49
8

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.

Christopher Wade Cantley
  • 7,122
  • 5
  • 35
  • 48