0xSina
0xSina

Reputation: 21593

iOS not using [email protected]

I have 3 images:

test.png
[email protected]
[email protected]

In IBOutlet, a UIImageView is set to display test.png.

On iPhone 3.5in without retina, it's displaying test.png

On iPhone 3.5in with retina, it's displaying [email protected]

But on iPhone 4in with retina, it's displaying [email protected]!!!

What's going on?

Thanks!

Upvotes: 9

Views: 4610

Answers (3)

Kevin Delord
Kevin Delord

Reputation: 2558

I know it's an old thread but I was having trouble with the new screen sizes for iPhone 6/6+.

What I did is to use this naming convention for different image files:

  • none if @1x small old phones
  • @2x for iPhone 4
  • -568h@2x for iPhone 5
  • -667h@2x for iPhone 6
  • @3x for iPhone 6 Plus

And then to automatically generate (full size) images just by including the code from this Gist in the project: https://gist.github.com/kevindelord/fe2e691d06ab745fbb00

You have nothing else to do. When you instantiate an image in your code:

[UIImage imageNamed:@"background.png”];

The categorised class from the Gist will automatically create an image corresponding to the current device.

There is a Pod for it UIImage+Autoresize documented on CocoaDocs.

Upvotes: 2

The following works for the iPhone. For the iPad, you would need additional images.

For the three versions of the background image, use the following names:

  1. background-480h.png (320x480)
  2. [email protected] (640x960)
  3. [email protected] (640x1136)

(You don't need a "-568h.png" image because there's no 320x568 iPhone screen.)

When you set the background image, simply append the screen height to the image name:

NSString* imageName = [NSString stringWithFormat: @"background-%ih", (int)[[UIScreen mainScreen] bounds].size.height];
[view setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: imageName]]];

iOS automtically appends the "@2x" if applicable.

You could omit the "h" after the height in the image names, but I think it's nice to emulate the iOS convention for the default image.

Upvotes: 7

rmaddy
rmaddy

Reputation: 318924

The -568@2x suffix only applies to the Default.png launch images. There is no special suffix used by UIImage imageNamed: (or the other UIImage methods). If you need a special image on the 4" screen, you need to add code to get the desired image yourself.

Upvotes: 9

Related Questions