How do I manage background images for the iPhone 5?
Asked Answered
R

2

12

Some of my apps use custom images as the background. What is the proper way to check the screen size to place the proper image?

Should it be something like this in viewDidLoad:

if ([UIScreen mainScreen] == 2.0)
{
     UIImage * backgroundImage = [UIImage imageNamed:@"[email protected]"];
     backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage]];
}
else
{
    UIImage * backgroundImage = [UIImage imageNamed:@"bgimage.png"];
    backgroundImageView = [[UIImageView alloc] iniWithImage:backgroundImage]];
}

Any tips/advice is much appreciated!

Thanks!

Rattigan answered 18/9, 2012 at 20:10 Comment(0)
G
16

The following code checks the size / bounds of the screen. If the screen is 586 points (remember, that the screen is measured in points because of Retina) than we know that it is the new 4-inch Retina Display.

if ([[UIScreen mainScreen] bounds].size.height == 568)
{
   UIImage * backgroundImage = [UIImage imageNamed:@"[email protected]"];
   backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
}
else
{
   UIImage * backgroundImage = [UIImage imageNamed:@"bgimage.png"];
   backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
}

See also iOS 6 apps - how to deal with iPhone 5 screen size?

Gongorism answered 18/9, 2012 at 20:23 Comment(3)
You can remove the "@2x" and .png text from the image file names and things should work. its usually not a good idea to put "@2x" in there since that's handled under the hood by imageNamed: anywaysMcburney
where would this go ? "- viewdidLoad"Twannatwattle
Yes. The code is basically what Luke put in the original question, where he is asking for something to put in viewDidLoad:Gongorism
I
1

Bryan's answer above worked for me, except that I had to use

[self.bgImageView setImage:backgroundImage];

rather than

backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];

The correct image only displays if I use the former.

Ingaingaberg answered 11/12, 2012 at 19:51 Comment(1)
backgroundImageView was just a name to illustrate how you write the if-statement. It was put in the question by Luke. Not something you are expected to find in your own program. By the same token, bgImageView is just a name in your program, won't work for anyone else unless they happen to have picked the same name.Gongorism

© 2022 - 2024 — McMap. All rights reserved.