15

In my iOS resource folder, I have the images:

foo~iphone.png
foo@2x~iphone.png
foo~ipad.png
foo@2x~ipad.png

I load them with:

NSString *fileName = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];

Experimentally:

On the iPhone, filename is /path/to/bundle/foo~iphone.png, and on the retina iPhone, it loads the @2x version.

On the iPad, filename is /path/to/bundle/foo~ipad.png, and on the retina iPad, it loads the @2x version.

That is, it loads the image I'd hope, but pathForResource handles the device modifier, and imageWithContentsOfFile handles the scale modifier.

I'd like to know why. The documentation of pathForResource says nothing about device modifiers, and the documentation of imageWithContentsOfFile says nothing about scale modifiers.

Is there separate documentation that indicates precisely what each function does?

Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
  • What value is your fileName string holding when you set a bp on the UIImage line? In other words how do you know the scaling is happening within the imageWithContentsOfFile? – Mark McCorkle Dec 20 '12 at 21:44
  • @MarkM, as I said, on the iPhone, `filename` is `/path/to/bundle/foo~iphone.png`, so I know it's only getting the name of the non-@2x version. When this name is fed into `UIImage`, I get the hi-res version. – Jesse Beder Dec 21 '12 at 14:28

1 Answers1

14

The explanation for imageWithContentsOfFile is in related documentation, not in the UIImage documentation itself:

On devices with high-resolution screens, the imageNamed:, imageWithContentsOfFile:, and initWithContentsOfFile: methods automatically looks for a version of the requested image with the @2x modifier in its name. If it finds one, it loads that image instead. If you do not provide a high-resolution version of a given image, the image object still loads a standard-resolution image (if one exists) and scales it during drawing.

When it loads an image, a UIImage object automatically sets the size and scale properties to appropriate values based on the suffix of the image file. For standard resolution images, it sets the scale property to 1.0 and sets the size of the image to the image’s pixel dimensions. For images with the @2x suffix in the filename, it sets the scale property to 2.0 and halves the width and height values to compensate for the scale factor. These halved values correlate correctly to the point-based dimensions you need to use in the logical coordinate space to render the image."

I don't know which documentation explains the behavior of pathForResource.

Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • Please quote from the documentation. I can't seem to find anywhere in either the overall `UIImage` documentation or the specific `imageWithContentsOfFile` documentation that talks about scaling. – Jesse Beder Dec 21 '12 at 16:29
  • And I'm also interested in why `pathForResource` handles the device modifier. – Jesse Beder Dec 21 '12 at 16:30
  • My answer has been updated to include quotes from the documentation talking about the different UIImage methods and why their actions do what they do. There is also discussion about the device modifier in there. If you are looking for more information on this I would recommend the WWDC videos as they provide examples and implementation of all these techniques. – Mark McCorkle Dec 21 '12 at 18:20
  • Thanks; I edited your answer a bit - feel free to tweak it if you don't like it. Any thoughts on the `pathForResource` issue? (I'm really looking for documentation, not videos of examples.) – Jesse Beder Dec 21 '12 at 18:43
  • I'll search around and see what I can find. I agree, Apple has a tendency to offer much more in depth explanations and techniques of their processes during WWDC than in their actual documentation. This has been a common theme of theirs since day 1 of the App Store in my experience. Although watching their videos can be a time consuming process I have definitely learned advanced techniques as well as good habits by watching their examples and following their logic path. Some examples are complex just to show different techniques to reach an end result even if it may not be the "best" solution. – Mark McCorkle Dec 21 '12 at 19:10