Patrice Cote
Patrice Cote

Reputation: 3716

Changing resources programmatically in Objective-C iOS 5 for Retina Display

My app have a background image that fills the screen. I'd like to display the correct .png file depending on if we're on a Retina Display device or not. I already have added all the .png files for both iPhone and iOS with the correct sizes. Is it possible ? If not, how should I handle it properly ?

I have XCode 4.3.2 with iOS 5.1 as the deployment target.

Upvotes: 0

Views: 2420

Answers (2)

pasawaya
pasawaya

Reputation: 11595

This probably would work. First it checks if the screen has a retina display, and if it does, sets the background image to the retina image. If it does not have a retina display, the background image is the regular image. You can put this in viewWillAppear or viewDidLoad.

if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
    ([UIScreen mainScreen].scale == 2.0)) {

    // Retina display
    UIImage *backgroundImage = [UIImage imageNamed:@"retinaImage.png"];

} else {

    // Non-Retina display
    UIImage *backgroundImage = [UIImage imageNamed:@"nonRetinaImage.png"];

}

Hope this helps!

Upvotes: 2

Michael Dautermann
Michael Dautermann

Reputation: 89509

If you name your graphics properly (e.g. add "@2x" to the suffix of the png files), iOS is smart enough to use your Retina display graphics on the appropriate devices & displays. Especially if you are using UIImageViews or controls or whatever, within your user interfaces designed within XIB files.

If you're doing programmatic images (i.e. where you define an outlet and then grab an image via something like "[UIImage imageNamed:], iOS is still smart enough to pick up the high rez images for you. Again, provided you named your graphics properly.

There are other questions here on StackOverflow that might help you out, such as:

How to support both iPad and iPhone retina graphics in universal apps

How to activate @2x high res graphics for retina display?

Upvotes: 4

Related Questions