MartinJeffers
MartinJeffers

Reputation: 47

iPad suffix for Images not working

I'm developing a universal app in xcode 5 and im trying to set the background image for both apps. The code I am using is in the viewDidLoad method:

    UIImage *backgroundImage = [UIImage imageNamed:@"city5.jpg"];
    UIImageView *backgroundImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
    backgroundImageView.image=backgroundImage;
    [self.view insertSubview:backgroundImageView atIndex:0];

The names of both images are city5~iphone.jpg and city5~ipad.jpg.

The images works as expected for the iphone. However the ipad image never loads, the view just stays blank. I'm deploying on an ipad 2.

Upvotes: 1

Views: 154

Answers (1)

Popeye
Popeye

Reputation: 12093

Your issue is in UIImage *backgroundImage = [UIImage imageNamed:@"city5.jpg"]; specifically imageNamed:@"city5.jpg" when you use imageNamed: it will look for a .png image not a .jpg image so in essence you are looking for file city5.jpg.png which clearly is what you want, so change that line to UIImage *backgroundImage = [UIImage imageNamed:@"city5"]; and change your image files to be .png.

If you want to leave it as .jpg try the below.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"city5" ofType:@"jpg"];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:filePath];

If none of these work I would suspect that your issue is one of three things:

  • The image you are trying to load doesn't exist in your bundle. So make sure the image is actually in your project and make sure the target is checked by clicking on the file and selecting the target it belongs to.

  • Make sure you don't have the image name spelled incorrectly.

  • Or you are using a retina display but do not have an @2x image. Try changing your simulator to retina and see if it shows up.

And as a last resort try doing UIImage *backgroundImage = [UIImage imageNamed:@"city5~ipad"];

You also try doing self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"city5"]];

Upvotes: 3

Related Questions