views:

27

answers:

1

I want to load an image from an array and set the view to display this image.

The current code isnt loading anything, just a gray image full screen.

myView.image = [UIImage imageNamed:[names objectAtIndex:r]]

If I have a counter r, which I use to access the index. How could it be done to load an image as such

myView.image = [UIImage imageNamed:@"3.png"];

but using r as the placeholder for the number, in this case 3.

If I have images 1.png to e.g. 10.png and want to use the r which I already increment and decrement, ow could I call the correct image using a formatted string in the place of

@"3.png" ?

Thanks in Advance.

+1  A: 

You can do:

myView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", r]];
pgb
+1 or perhaps "%d" instead of "%@", if `r` is an int instead of an `NSNumber`.
David Gelhar
You are right, I meant to write %d. Just changed it.
pgb
That doesn't seem to work. It crashes the app. The r counter works correctly, and if I hardcode e.g.myView.image = [UIImage imageNamed:@"9.png"];or any other image in the same manner, it works perfectly. As well as loading from the array of images doens't work correctly either.Not sure what the problem is.
alJaree
Thanks a lot to both of you , replied earlier before refreshing in the other comments. The @ was crashing the app, tried the %d and everything works perfect. Thank you so much. :)
alJaree
Please try to accept the answers that work for you (the big tick). With your current accept rate (17%), a lot of people will be reluctant to answer you.
pgb
just got back to this page sure. I will do that. :)
alJaree
Try to review all your questions, and check the answer that solved your issues, so your accept rate is better.
pgb