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.

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”.

2 comments for “Automatically loading iPhone 5-sized images where required

  1. Poojan
    2012/12/06 at 4:43 PM

    Hi Dear,

    Does ‘method_exchangeImplementations’method reject by apple while submitting application in apple store?
    Please review “http://stackoverflow.com/questions/7720947/method-swizzling-in-ios-5″ and let me know.

    Thanks in advance.

    Regards,
    PS

  2. noradaiko
    2013/03/22 at 2:58 PM

    @Poojan

    Hi, I’m using this code in my app but I haven’t ever been rejected from apple.

    Cheers,
    nora

Leave a Reply

Your email address will not be published.