How to minimize code when using different images for iPad and iPhone?

I almost finished the iPhone version of my application when I realized that I would have to (possibly?) Write legend for each user interface element, because they use different graphics. This will make my code very dirty. I have 15 different user controls for the user interface, and I can't imagine setting up a conditional ("if ipad load image_ipad, if iphone load image_iphone") for each method is the best way to do this. Can anyone suggest a specific technique to solve this problem? Or is there a way to name the images, how can you between the retina and the regular?

+7
source share
2 answers

You just need to pass the main name of the image. Say our image name is "Apple.png", then you write the name of the image recipient like this UIImage *image = [UIImage ImageNamed:@"Apple.png"] , and the name for the image for the following cases will be:

1. iPad > put ~ ipad, the image name will be "Apple~ipad.png"

2. iPhone > install ~ iphone, the image name will be "Apple~iphone.png"

3.for retina display> put @ 2x, the image name will be " Apple@2x.png "

4. If you have a diffrenet image for the retina for ipad and iphone, it will look like iPad " Apple@2x ~ipad.png" , for iPhone " Apple@2x ~iphone.png"

If you name it like this, iOS will detect the best image image name and load it.

+7
source

If you want to have versions for the retina and not the retina, you just need to keep two versions of your asset in your kit. If your asset is called button.png, you save:

-button.png for the version without the retina. -button@2x.png for the retina display version.

Your code:

 UIImage * myImage = [UIImage imageNamed:@"button.png"]; 

The system automatically checks for @ 2x.png if there is a retina, or it will choose another for non-retina.

You can put myImage in your user interface, and the OS will do everything for you; -)

+2
source

All Articles