tags:

views:

115

answers:

3

My code is listed below. How can I do it?

UIImage *image = [UIImage imageWithContentsOfFile: 
                  [[NSBundle mainBundle] pathForResource:@"back" 
                                                  ofType:@"png"
                                             inDirectory:@"data/ui/button"]];
A: 

As the article linked mentioned, use [UIImage imageNamed:@"image.png"] and make sure your images are in the root of the bundle. I recommend against using the category in the article as Apple will make sure that imageNamed will load the proper image even if they goto a 4x image at some point in the future; the category makes assumptions about the device hardware.

Scott Gruby
I have several considerations: 1. Image files loaded by imageNamed: will be cached, and never released. I choose imageWithContentsOfFile: to save the memory footprint; 2. My app has almost 100 UI resource images (and the number will be doubled with @2x files), is it a good choice to put all of them to the root of the bundle? 3. Yes, the solution proposed in the article is not perfect.
MQ Gu
A: 

Instead of imageWithContentsOfFile: method, why don't you use imageNamed: method?
I only use this for my images and they are correctly loaded for iPhone 4...

Matthew
As I mentioned in another comment, the reason is: Image files loaded by imageNamed: will be cached, and never released. I choose imageWithContentsOfFile: to save the memory footprint.
MQ Gu