tags:

views:

72

answers:

3

This is driving me nuts. Been searching for 2 days, and I can't find any real solution or explanation for why this is happening. I know there are threads here on SO, as well as some other places, but they have been no help. I have read the Apple documentation on the matter.

I have normal and @2x images in my app. They are named correctly (edit_image.png, and [email protected]). They are sized correctly (normal is 60x60, @2x is 120x120). They are both being copied into the app bundle, and when I examine the contents, I can see them both in the root.

I am grabbing the image by calling [UIImage imageNamed:@"edit_image"]. It never grabs the 2x image. It only sees the 1x image. However, if I check the scale first, and use this code:

if ([[UIScreen mainScreen] scale] == 1) {
    NSLog(@"test");
    editImage = [UIImage imageNamed:@"edit_image"];
} else {
    editImage = [UIImage imageNamed:@"edit_image@2x"];
}

Then it does grab the correct image. I have tried everything. I deleted the high res from the project, cleaned, re-added the high res, cleaned and then built, no dice. I deleted all the images, and re-added them, no dice. I have done everything I can think of. What the hell is going on here?

A: 

You don't need to add the "@2x" bit, or have the if-else logic at all. Just use [UIImage imageNamed:@"edit_image"].

Shaggy Frog
But OP said that didn't cause the `@2x` image to be picked up automatically.
BoltClock
Exactly. Thats the problem. I know the if-else logic is redundant. But I can't pick up the `@2x` image any other way.
Gordon Fontenot
A: 

Two silly mistakes that I've made 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
Not making these mistakes. When I explicitly load the @2x versions, I get the right image.
Gordon Fontenot
+1  A: 

Are you creating universal application for both iPhone & iPad. If universal app is there then you need to create 3 set of images:

1) edit_image~iPad.png

2) edit_image~iphone.png

3) edit_image@2x~iphone.png

each with the same resolution of 72 pixels/inch. While you need to provide double size for that @2x image which I think you've already done this.

Now, try the below code

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"edit_image" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];

Important: When creating high-resolution versions of your images, place the new versions in the same location in your application bundle as the original.

Source: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/SupportingResolutionIndependence/SupportingResolutionIndependence.html

Sagar
No, it's not an universal app. It's iPhone, iPod only. And the high res versions are in the exact same place as the originals.
Gordon Fontenot
Sagar
This category of UIImage class would help here I think: http://github.com/eisernWolf/TouchCustoms/blob/master/Classes/UIImage%2BUniversalApp.h http://github.com/eisernWolf/TouchCustoms/blob/master/Classes/UIImage%2BUniversalApp.m
Sagar