Reputation: 1
I have loaded an array:
currentBackground=4;
bgImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"mystery_01 320x460"],
[UIImage imageNamed:@"mystery_02 320x460"],
[UIImage imageNamed:@"mystery_03 320x460"],
[UIImage imageNamed:@"mystery_04 320x460"],
[UIImage imageNamed:@"mystery_05 320x460"],
nil];
Now I want to display in a label the file name of the image currently being displayed. I thought:
mainLabel.text = [NSString stringWithFormat:@"bgImages= %@",[bgImages objectAtIndex:currentBackground]];
would work but all I get is hex code . I have a button that scrolls through the images very nicely. But when I try to display the image name all I get is what I believe to be the address where the name resides.
Upvotes: 0
Views: 247
Reputation: 8497
The file name of the image is not stored in your UIImage object. There is no such property in the UIImage class. You'll have to store the names yourself. Perhaps you could store the file names in an array, and use this array both to retrieve the images from file and then to look up the image file names later:
imageFileNames = [[NSArray alloc] initWithObjects:
@"mystery_01 320x460",
@"mystery_02 320x460",
@"mystery_03 320x460",
@"mystery_04 320x460",
@"mystery_05 320x460",
nil];
Retrieve the images (you could use a for-in loop for this instead if you wish):
bgImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:[imageFileNames objectAtIndex:0]],
[UIImage imageNamed:[imageFileNames objectAtIndex:1]],
[UIImage imageNamed:[imageFileNames objectAtIndex:2]],
[UIImage imageNamed:[imageFileNames objectAtIndex:3]],
[UIImage imageNamed:[imageFileNames objectAtIndex:4]],
nil];
Get the file name from your file names array:
mainLabel.text = [NSString stringWithFormat:@"bgImages= %@",[imageFileNames objectAtIndex:currentBackground]];
Upvotes: 2
Reputation: 9906
Using %@
in your string format effectively just calls the object's description
method. The hex value you are seeing is just the value of the object pointer - this is NSObject's default implementation of the description
method.
Note that for an NSString objects, the description
method returns... You guessed it - the NSString object itself.
In your case, to display the image names, you have to keep them, as it seems UIImage's description
doesn't return the image name.
You can also make a UIImage subclass which remembers the name, but not sure it's worth the hassle.
Upvotes: 0