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

投稿者:

Takuya

Digital crafts(man|dog). Love photography. Always making otherwise sleeping. born in 1984.

“Automatically loading iPhone 5-sized images where required” への2件のフィードバック

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

    いいね

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Google フォト

Google アカウントを使ってコメントしています。 ログアウト /  変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中