Using numbered image on UIImage with NSString with format

When listing images on UIImageView, there’s several way to name your image and show it. On this post, we’ll take a look at 2 ways on doing it. First is simply using ascending integer without digit limit. (e.g. image1.png, image2.png, …. , image112.png, etc). Second is limiting digits. (e.g. image001.png, image002.png, … image112.png, etc).

First way is as below.

int imageNumber = 1;
[UIImage imageNamed:[NSString stringWithFormat:@"image%i.png", imageNumber"]];

And the second way.

int imageNumber = 1;
[UIImage imageNamed:[NSString stringWithFormat:@"image%03d.png", imageNumber"]];

03 means 3 digits. use 02 for 2 digits and so on. :D

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

HTML tags are not allowed.