0

When setting the image for a button, I use stringWithFormat: like so:

 [buttonImage setImage:[ImgUtil image:[NSString stringWithFormat:@"myImage_%d.png", selectNum + 1 ]] ];

I want to inspect that string. I thought maybe I could get the name back from the button:

 if (buttonImage.image == [UIImage imageNamed:@"myImage_2.png"]) {
        NSLog(@"the name of the buttonImage is %@", buttonImage.image);
 }

but that doesn't work. How can I look at that string?

jscs
  • 63,694
  • 13
  • 151
  • 195
hanumanDev
  • 6,592
  • 11
  • 82
  • 146
  • 1
    possible duplicate of [How to get Image Name used in an UIImage?](http://stackoverflow.com/questions/10279314/how-to-get-image-name-used-in-an-uiimage) which I answered – Evan Mulawski May 28 '12 at 15:29
  • 1
    @EvanMulawski: I don't think it is a duplicate. – nhahtdh May 28 '12 at 15:32
  • 1
    @nhahtdh: Yes, it is. He wants to get the name of the image represented by the `UIImage` object. – Evan Mulawski May 28 '12 at 15:33
  • @EvanMulawski: The question may be the same, but the purpose and the setting is different. – nhahtdh May 28 '12 at 15:38
  • 1
    @nhahtdh: The question title is the same, the question itself is the same, and the end result is the same. Therefore, it is the same question. – Evan Mulawski May 28 '12 at 15:39
  • I've edited this post pretty heavily because, based on the accepted answer, and the comment below it, it was completely misleading. Turns out it wasn't a dupe, @Evan, although I agreed with you until I saw the OP's comment. – jscs May 28 '12 at 17:08
  • @JacquesCousteau: Wow, the OP's entire original question was misleading. – Evan Mulawski May 28 '12 at 17:17

5 Answers5

3

You could use associated references to attach a string the key "name" at load time. You create the UIImage from a file, and attach the name using the objective-c associated references API: here.

You can also sub-class UIImage to store an extra name.

You can even add a category to provide an easy API.

Cthutu
  • 8,713
  • 7
  • 33
  • 49
  • I think associated reference may be a good solution. I don't like sub-classing for such a trivial task, though. – nhahtdh May 28 '12 at 15:42
2

If what you want is to "test what the "myImage_%d.png" ends up being" in the following line:

[buttonImage setImage:[ImgUtil image:[NSString stringWithFormat:@"myImage_%d.png", selectNum + 1 ]] ];

Then I would suggest that you reformat and simplify your code. It will give you the additional advantage of making it easier to read:

NSString* imageName = [NSString stringWithFormat:@"myImage_%d.png", selectNum + 1 ];
NSLog(@"imageName is %@", imageName);

[buttonImage setImage:[ImgUtil image:imageName]];
diegoreymendez
  • 1,997
  • 18
  • 20
  • that was what I was looking for. worked great. thanks for your help! – hanumanDev May 28 '12 at 15:51
  • @hanumanDev: All you wanted was the value of that string?! This question is completely misleading. `UIImage` has nothing to do with it. – jscs May 28 '12 at 17:03
2

No, you can't.

buttonImage.image is a UIImage stored in memory inside the button.

[UIImage imageNamed:@"myImage_2.png"] creates an entirely different UIImage. Both UIImages could very well have been created from the same file--in this case, @"myImage_2.png"--but they are two separate UIImages in memory.

The == check in your line:

if(buttonImage.image == [UIImage imageNamed:@"myImage_2.png"])

Does not check if the UIImages were created from the same file; it checks if they are pointing to the same location in memory. Which they are not, because they are two separately created and stored UIImage instances.

--

So, no--you cannot do this. Something that might solve your problem another way, though, is to subclass UIButton and add a properly NSString* imageFilename. (If you're setting different images for each control state, you'd need more than one variable to store those image file names in). Then override the setImage:forControlState method of the UIButton subclass and store the filename there every time the image is changed. Then you can perform the following check:

if([imageFileName isEqualToString:[[NSString stringWithFormat:@"myImage_%d.png", selectNum + 1 ]])

And that would get you the answer you want!

WendiKidd
  • 4,333
  • 4
  • 33
  • 50
1

You can store the UIImage as instance of the class, and compare it. You won't be using more memory than a pointer.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
0

selectNum stands for the selected image, right?If so, try to get selectNum when picking the picture.

anna
  • 662
  • 5
  • 28