0

Both files "flipImage.png" and "flipImage@2x.png" are in project. In -[FlipsideViewController viewDidLoad] I have the following code. The sanity check (thanks to other stackoverflowers) correctly reports retina or no. But in either case, the image loaded is the small one, verified by its height. Why no auto-load of appropriate image? OK, I can see workarounds, but I'd like to Use The System when possible.

UIImage* flipimage = [UIImage imageNamed:@"flipImage.png"];
NSLog(@"Image height = %f", flipimage.size.height);  // always 416, never 832 :(

// Sanity check.
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale == 2.0)) {
    NSLog(@"Retina");
} else {
    NSLog(@"Non-retina");
}   
Andrew Duncan
  • 3,553
  • 4
  • 28
  • 55

2 Answers2

1

iOS retina displays don't work that way. The height of an @2x image and a standard resolution image will be the same on the device and in your code. When you create an image on the screen that is 416 points x 416 points, it doesn't change height just because it's on a retina display versus a non-retina display.

The difference is that @2x images have higher resolutions so they show more pixels per point which is what the retina displays are using instead of pixels.

So essentially, all you need to do is use the standard resolution filename for any image you use within the app and the OS will automatically take care of replacing it with the higher resolution images if it's on a retina display.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
  • Thanks, apparently checking the "size" of the image does *not* correctly tell me which file was loaded. In fact, the right thing is done. I need to study more the evolving model and the distinction between LOGICAL size (pixels) and DISPLAY size (points?). This is all reminiscent of aspect-ratio struggles, which I once sorted out in an epic rant at http://www.andrewduncan.ws/aspect. furrfu! – Andrew Duncan Apr 29 '13 at 23:51
  • @AndrewDuncan In actuality, once an image is loaded into a `UIImage`, you can't really know which file was used to instantiate it anyways, regardless of checking the size, etc. If my answer was helpful to you, please check the checkmark next to it so it will close out this question. – iwasrobbed Apr 30 '13 at 00:33
  • If you want to know the size in physical points and not logical points, you can ask to the CGImage referenced in UIImage. UIImage is just a wrapper around CGImage. You can also ask to UIImage about his scale, usually in a retina display if you have provided an @2x image is equal to 2. – Andrea Apr 30 '13 at 06:07
0

According to the previous comments... and without having to change your code a lot, why don't you just do this:

UIImage* flipimage = [UIImage imageNamed:@"flipImage.png"];
NSLog(@"Image height = %f", flipimage.size.height * [UIScreen mainScreen].scale);

That should returned you the size (Number of points * number of pixels per point).

crisisGriega
  • 852
  • 7
  • 17