Reputation: 21593
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
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:
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
Reputation: 3597
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:
(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
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