Automatically loading iPhone 5-sized images where required

You’ll be familiar with the way that [UIImage imageNamed:] automatically loads @2x versions of images when running on a retina device.
Unfortunately, imageNamed: will NOT automatically load -586h@2x versions of images when running on an iPhone 5.

Sometimes this doesn’t matter, for example icons and non-full screen graphics are probably the same on iPhone 4 & 5. However, if you have full-screen background images, or full-width / height background images for toolbars etc you will have problems. Your 480-high images will most likely get stretched (and will probably look horrid as a result).

There is a better way to load automatically -586@2x versions of images(if available) than manually checking the screen size.
Here is a category named Retina4 of UIImage to do exactly this thing.

https://gist.github.com/3784303.js?file=UIImageRetina4.h

https://gist.github.com/3784303.js?file=UIImageRetina4.m

This code is originally made by Benjamin Stahlhood though I fixed below problems that are existed in original version.

  • Replacing imageNamed: method for retina4 is executed multiple times. So retina4ImageNamed: is never called in some cases.
  • The scale property of UIImage is set to 1.0f instead of 2.0f because the filename is specified with “@2x”.