Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

leo kamin
leo kamin
5,550 Points

Cannot get My app to work on the retina

I am adding a background image, when I used the attributes inspector my app would work on the retina and non retina displays but when I hard-coded it It would only work on the 3.5 inch display. Here's my code:

UIImage *image = [UIImage imageNamed:@"photo"];
UIImageView *bg = [[UIImageView alloc]initWithImage:image];
[self.view insertSubview:bg atIndex:0];

1 Answer

Mike Baxter
Mike Baxter
4,442 Points

Are you saying it won't work on a 4-inch display, or it won't work on any Retina device? Because they're two very different issues.

To work on a 4-inch display like the iPhone 5, you need to set a 4-inch Launch Screen image. Otherwise it restricts the app to only working on 3.5-inch devices.

Remember that all of your Retina images should be named something like "my_image@2x.png." When you use the UIImage imageNamed method, the app checks if it's a Retina device and then proceeds to look for the image with the "@2x" in the name. Otherwise it just returns the image name.

So suppose you have an image called "photo.png", you'd need a Retina version of it called "photo@2x.png" also included in your app's Main Bundle, and then your code to load it would look like this:

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

But that imageNamed method requires you to have "photo" stored in the app's Main Bundle (which happens when you just drag a photo into Xcode and check off the box to add it to the bundle). Maybe it's not working because it's not included?

leo kamin
leo kamin
5,550 Points

It is just no working on the 4in the problem is with the different sized screens how would I load an image for the 4 inch display?

Mike Baxter
Mike Baxter
4,442 Points

Here's a tutorial that includes information on how to do this; you'll have to go down to the section that says "Setup Up App."

It's a lot easier to reference that tutorial (because it has images) than to try to describe it on the forum here.

Mike Baxter
Mike Baxter
4,442 Points

You basically drag and drop a 4-inch Launch Image into that "Launch Image" spot you see in Xcode.

leo kamin
leo kamin
5,550 Points
            I am trying to make a background image as opposed to a launch image.
Mike Baxter
Mike Baxter
4,442 Points

leo kamin , you still have to have the Launch Image, or else your app will only load at 3.5-inch display and leave horrible black bars at the top and bottom. Basically, the Launch Image tells the app what size to make the display at the time of launch, before you ever get to the question of making a background image for a particular display. So you have to have both the Launch Image and the particular background image to properly do what you want.