7

In my FirstViewController I have write this code for switch background if device is iPhone4 or iPhone5:

Filename: bg-for5-568@2x.png
bg-for4@2x.png
bg-for4.png

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *backgroundImage = [[UIImage alloc] init];
    if([[UIScreen mainScreen]bounds].size.height == 568)
    {
       backgroundImage = [UIImage imageNamed:@"bg-for5-568h"];
    }
    else
   {
       backgroundImage = [UIImage imageNamed:@"bg-for4"];
   }
   self.view.backgroundColor = [[[UIColor alloc] initWithPatternImage:backgroundImage] autorelease];
   [backgroundImage release];
}

When i lanch the app on my simulator, the background for iphone5 show double size, out of the view.

/* RESOLVED THANK YOU */

Jimmy
  • 119
  • 1
  • 2
  • 7
  • Look that: http://stackoverflow.com/questions/12446990/how-to-detect-iphone-5-widescreen-devices – Marco Pace Oct 19 '12 at 10:47
  • 2
    It will double size because it IS double size and iOS will stretch it so that 1 pixel of the image will be displayed in 1.0 "pixels" of the grphic pane which stretches over 2 hardware pixels on retina devices. There are several ways of dealing with the issue. The easiest is to change "bg-for5-568h@2x" in that line of code to "bg-for5-568h" but do not change the name of the png file. – Hermann Klecker Oct 19 '12 at 11:03
  • Thank you very much! Now perfect works. – Jimmy Oct 19 '12 at 11:11

3 Answers3

5

I am not sure if this is the solution for this problem as I am missing some infos, but:

At first: Strip the .png from your imageNamed: method. Since iOS4, you shouldn't do this anymore. The next thing is: What are the Names of your image? Note that an iPhone5 has a retina display, and your image should be named bg-for5-568h@2x.png but referred in the sourcecode as bg-for5-568h.

Besides that: In almost every case where your image isn't a photograph, what you are doing is a bad idea. And even if it is a photograph, simply use the bigger image for the iPhone 4 and 4S as well. It's not that much bigger, so the memory footprint isn't a problem here! Have a look on UIImageView's contentMode property. You can use this to adjust the position of the larger image. You also might want to check UIImageViews clipSubviews property to clip the image if it isn't fullscreen.

Trust me, in my company we had a loot of hooks for stuff like ~ipad, ~iphone, ~2x and even stretchable images. And all these hooks worked fine till the date, apple announced something similar or simply a new device. So I decided to not do these kind of hooks anymore. They seem to be very helpful in the first place, but the trouble you get when there is something new on the market isn't worth it!

Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
  • i have edited my code, i have a different background for each view. I don't know because in the iphone5 background show more big, out of the view. – Jimmy Oct 19 '12 at 11:01
  • 1
    Strip the `@2x` in your edited code! Just the image should be named with `@2x`, in the code, just put the file name! – Michael Ochs Oct 19 '12 at 11:10
2

You should append @2x suffix to all of your retina images. In your case your image should be stored as "bg-for5-568h@2x.png". Hope it will resolve the issue.

kidsid49
  • 1,358
  • 3
  • 18
  • 37
1

I would not advise doing this, what if Apple change their screensize again and you have to go back and rewrite all your code?

A simple fix is to use:

  self.view.backgroundColor = [UIColor colorWithPatternImage:/* your image*/];

This could give you some issues with stretching or tiling.

I prefer using

 UIImage* imageName = [[UIImage imageNamed:/*image name*/]resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)];

In iOS 6 you can improve this further by defining if you want the image to stretch or tile. This allows you to create a border which won't change and then the centre of your image by default being tiled and filling the space of your imageview

Matt Rees
  • 884
  • 8
  • 14
  • this works only if you have to support ipad and iphone OR iphone. This doesn't work for iphone 4 AND iphone 5 (damn Apple!) – Marco Pace Oct 19 '12 at 10:47
  • It works for me on iPhone 4 and iPhone 5. You need to set the frame of your imageview to self.view.bounds – Matt Rees Oct 19 '12 at 10:53
  • Mmm, reading the apple reference I didn't find it, does it work with Xib too? – Marco Pace Oct 19 '12 at 10:56
  • i have edited my code, i have a different background for each view. I don't know because in the iphone5 background show more big, out of the view. – Jimmy Oct 19 '12 at 11:03
  • https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html resizableImageWithCapInsets:resizingMode: – Matt Rees Oct 19 '12 at 11:08