views:

475

answers:

6

I have a TabBarController that sets the image for the tab like so, in the -init method:

self.tabBarItem.image = [UIImage imageNamed:@"tabImage.png"];

I have a [email protected] file in the resource. In the iPhone 4 simulator or the phone, the hi-res image isn't being picked up - the low res version is simply being scaled up.

Any ideas why this might be?

EDIT: Some more info: If I try and explicitly use [email protected] (or just tabImage@2x) then the tab image I see is extremely large and blown up beyond the bounds of the tab, as if it's being scaled from 60px to 120px. So it looks like whatever name is supply is being treated as a scale=1.0 image.

A: 

You can leave the .png off now. I believe it will still work, but you may try that.

Joshua Weinberg
That doesn't seem to affect my problem
psychotik
what does `NSLog(@"%f",[[UIImage imageNamed:@"tabImage.png] scale);` print
Joshua Weinberg
Jushua - it prints 2.000000
psychotik
then the image is 2x and is loading appropriately. Are you positive its being scaled up?
Joshua Weinberg
Yes, I'm sure it's not. I changed the image completely to be a 60x60 image different from the 30x30 image, and I still see the original scaled up.
psychotik
You're doing something other than what you're showing us then. if the code above is printing what you told me, and the code you placed in the original question is correct, the 2x image is being loaded.
Joshua Weinberg
+1  A: 

Are you sure the file has been added to the XCode project and is visible in the project explorer?

xmr
Yes, I'm positive.
psychotik
A: 

I just went through a few hours of redoing art in The Gimp and trying to get it recognized and loaded by my app on an iPhone 4.

I ran into the problem described with certain images with a @2x extension not being recognized and loaded.

I was not able to discern any pattern. My images are all loaded using [UIImage imageNamed:@"<name>.png"] into a singleton. I inspected the image scale settings post-startup and some were 1.0 (the old art) and some were 2.0 (the new art).

The only way I was able to resolve this problem was to delete and re-add the high resolution images that were not being recognized.

Jonathan Watmough
A: 

Two silly mistakes (both of which I've made before) that can cause this problem:

  1. Accidentally naming the small versions @2x instead of the large ones
  2. Having the large versions be slightly missized (by one pixel)
Jon Rodriguez
A: 

At least in 4.1, if you use:

[UIImage imageNamed:SC==1?@"star.png":@"[email protected]"];

it will not work, the image does not have the right scale. Use this instead:

[[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:SC==1?@"star":@"star@2x" ofType:@"png"]] autorelease];

Ali Fracyon
A: 

Always use

 [UIImage imageNamed:@"foo.png"]

This will work on 3.x and 4.x devices, and on the 4.x Simulator. Devices with Retina Displays (and the 4.x simulator) will magically pick up the @2x versions of your images; iOS has been modified to be smart about this function and @2x.png files.

Make sure you have both the @2x.png and the normal.png added to the project file, and do a full clean & build. As others have mentioned, verify the size of the images, too; apparently if they're not exactly 2x the dimensions it won't work (I haven't verified this myself).

If you leave the .png off, it will only work on iOS 4.0. So if you're building a 4.0+ only app, you can ask for:

    [UIImage imageNamed:@"foo"]

If you have only one image and want to use it on both device versions, then you'll have to change view.contentMode to scale to fit. Take a look at other questions for that. (I'll try to edit to include a link)

AndrewS