1
votes

I am on my voyage to supporting the iPhone 5. I am well aware that there is iOS 6's auto layout feature in Interface Builder. This is great however, sometimes I want to load my own images if its an iPhone 5.

So some of the images in my game for iPhones are exactly the screen's size either (320x480) or (640 x 960). So if I am on an iPhone 5 those images will not cover the entire screen. So I am going to make (640 x 1136) images for those full screen images.

Now I know that if I simply name the image: [email protected] it won't load. So, how would I subclass UIImage so that it will load the -568h@2x image thats not the default image?

Thanks!

1

1 Answers

1
votes
  1. The lazy way:

    UIImage * img = [UIScreen mainScreen].bounds.size.height > 500 ? [UIImage imageNamed:@"Default-568h.png"] : [UIImage imageNamed:@"Default.png"];
    

    I've done this in an app where the main screen on devices without a camera is Default.png; it doesn't seem too hacky because we would need to change Default.png to support new devices anyway.

  2. The less lazy way: Condition on the space you need to fill, not the screen size.

  3. The solution you asked for isn't really generic enough to warrant a class, but here it is anyway:

    @implementation MyImage
    +(UIImage)imageNamed:(NSString*)name {
      if ([UIScreen mainScreen].bounds.size.height > 500) {
        UIImage * img = [UIImage imageNamed:[name stringByAppendingString:@"-iPhone5"];
        if (img) { return img; }
      }
      return [UIImage imageNamed:name];
    }
    @end
    

    There's really no need to subclass UIImage. If you're feeling really hacky, you can patch/swizzle/otherwise +[UIImage imageNamed:] to point to a custom method, but I really don't recommend that except for debugging.