imageWithContentsOfFile does not find hi-dpi version of image

Originator:mundue
Number:rdar://8160592 Date Originated:05-Jul-2010 08:53 PM
Status:Duplicate/8084451 Resolved:
Product:iOS 4 Product Version:8A293
Classification:Important Reproducible:Always
 
05-Jul-2010 08:53 PM Matt Martel:
Summary:

In iOS 4.0 on a new iPhone 4, calling imageWithContentsOfFile: does not load the appropriate hi-dpi version of a file.

Steps to Reproduce:

My code looks something like this:

[images addObject:[UIImage imageWithContentsOfFile:[[path stringByAppendingPathComponent:m_Label] stringByAppendingPathComponent:[imagePaths objectAtIndex:0]]]];

imagePaths is a dict of file names, like "red.png". In the subdirectory specified by m_Label I have placed both "red.png" and red@2x.png".

Expected Results:

I expect the resource named "red@2x.png" to be displayed on the iPhone 4.

Actual Results:

It shows "red.png" instead.

Notes:

If I specify "red@2x.png" instead it correctly displays that file.

Comments

Guys,

The documentation is very clear. On iOS 4.0 you can expect the imageWithContentsOfFile method to automatically find the @2x version. Apple is admitting this is a known issue, note the dupe issue number.

Also, omitting the file suffix will not work on pre-4.0 devices, so that is not going to work. Additionally, omitting the file suffix for this method also fails on iOS 4.0. I have tried it.

Thanks

Perhaps the documentation should be updated to reflect this fact? As it stands, the documentation implies that initWithContentsOfFile: and imageWithContentsOfFile: will return @2x versions of images without any modifications to code, regardless of whether the extension is specified or not.

By james.addyman at July 18, 2010, 11:33 p.m. (reply...)

agreed, i would consider it to be unexpected behavior if i give the api a full path for it to return data from some other path. Use imageNamed: or otherwise wrap your call with a check against [UIDevice currentDevice].scale when building your path.

This sounds like expected behavior: it is giving you exactly what you asked for since you are giving it a complete path, not just a name.

Are you making sure to leave off the ".png" file extension in your imagePaths? If you specify an extension, it won't find the @2x image.

By lucius.kwok at July 18, 2010, 7:34 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!