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 Answer 1

2

You can do:

myView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", r]];
Sign up to request clarification or add additional context in comments.

4 Comments

+1 or perhaps "%d" instead of "%@", if r is an int instead of an NSNumber.
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.
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. :)
just got back to this page sure. I will do that. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.