UIImage & The Retina display

If you were previously loading a UIImage with initWithContentsOfFile, you will need to change your code slightly if you want to support the high resolution capabilities of the iPhone 4 retina display. For some reason you must now load an image with imageNamed if you want iOS 4 on an iPhone 4 to auto select the high res image with the @2x file extension.

I have contacted Apple regarding this issue and have yet to hear back from them. I will let you all know what they say.

Previously you could have done the following:

	NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"image.png"];
	UIImage *image = [[UIImage alloc] initWithContentsOfFile: imgPath];

You must change your code like this:

		UIImage *image = [UIImage imageNamed:@"image.png"];

NOTE: If you want your app to also work on iPhone OS 3 you must include the image extension such as ‘.png’ but you are no longer required to do so with iOS 4 only apps.

Random Posts:

If you found this useful, shoot me a small donation or at the very least leave a comment, every bit of encouragement helps keep me motivated to update with more content on a regular basis!

Comments

*

8 Responses to “UIImage & The Retina display”

  1. [...] to support the retina display on the iPhone 4. It was quite simple really, just had to change a few lines of code, edit my Entitlements.plist file, and add high resolution [...]

  2. Dennis Hopper says:

    The new way is much simpler though. Good move, but did they have to break initWithContentsOfFile? Why not just deprecate it? :/

  3. [...] other day I posted about a problem with using initWithContentsOfFile to load a high res image on the iPhone 4. At the same time I submitted a bug report to Apple. Today [...]

  4. Interesting bug.
    But if the other method (image named) was an option before why wasn’t it the common practice before as it’s so much simpler?

  5. Ramon Fritsch says:

    So that means you have to include image.png(normal res) and image@2x.png(high res) on your project?

    Not clear for me, sorry.

  6. bob says:

    imageNamed: is bad for me because it caches the images in memory (and there is no way to clear out the cache myself). What if I don’t want to cache my images?

  7. rlrcstr says:

    What if your image is not in the main bundle? This doesn’t seem to work when pulling an image from the documents folder.

Twitter Me