views:

105

answers:

2

I have 3 versions of an image: "myImage~ipad.png", "myImage~iphone.png" and "myImage@2x~iphone.png".

Obviously, the last one has twice the size of the second.

I am loading them using

UIImage *imageU = [UIImage imageNamed:@"myImage.png"];
UIImageView *myView = [[UIImageView alloc] initWithImage:imageU];
[self.view addSubview:myView];
[myView release];

the image loads fine on the iPad and 3G/3GS but not on iPhone 4, where it appears huge, with twice the size. iPhone 4 is doubling the size of "myImage@2x~iphone.png" ??? (!!!)

What can be wrong?

thanks.

+1  A: 

It does not, you are doing something wrong, check the rect of the imageview, a @2x image on iphone4 has the same coordinate points and size as the smaller image on older phones

NSLog(@"%f %f %f %f",myView.frame.origin.x,myView.frame.origin.y,myView.frame.size.width,myView.frame.size.height);

valexa
this command shows me 0,0,800,100 that is the size of the image being loaded. Wasn't this supposed to be shown in points? This should be shown as 0,0,400,50... (the smaller image has 400x50 pixels and the hires image has 800x100, obviously... this is what is being reported)... why?
Digital Robot
@valexa Instead of that long and hard-to-remember NSLog call you did there I'd recommend using some of the helper functions: `NSLog(@%"%@", NSStringFromCGRect(myView.frame));`. There are other `NSStringFrom*`functions available to do this stuff more easily.
bddckr
@bddckr using NSStringFromCGRect would not explicitly illustrate to the OP what those values printed stand for
valexa
@OP try NSLog(@"%fx%f scaled by %1.1f",myView.frame.size.width,myView.frame.size.height,myView.contentScaleFactor);The scale should be 1.0 in both cases which means no scaling was performed and the proper images were loaded
valexa
the result, as expected is 800x100 scaled by 1.0... but if I grab a screenshot and measure the image size on Photoshop it has 1600x200!!!!!!!!!! When I run on 3GS, the result is 400x50 scaled by 1.0
Digital Robot
@valexa Makes sense, I'll leave it here for others.
bddckr
Mike that makes no sense, can you take a screenshot of your desktop while it has the screenshot from the 4 device or the 4 emulator open.
valexa
see here... http://imgur.com/0HZOA.jpgsee how the orange bar is huge on iphone 4... and believe me, the image being loaded is just twice as large as the 3GS one, as it should be. See how the background image loads perfectly on iPhone4 but not the bar... this is very strange... my brain is burning...
Digital Robot
I notice not only the bar is doubled but also the buttons,it looks like you are scaling a further more 2x the whole view that contains both the buttons and the bar.Basically it it resized by your code after the point in which you did the nslog.
valexa
you guys will not believe this, but I have discovered that contrary to Apple docs, you cannot name the image as "myImage@2x~iphone.png"... you have to name it "[email protected]"... using the @2x at the end. I have answered this question describing the whole solution. Thanks for all help. I am giving points to you guys!!!!
Digital Robot
+2  A: 

Unbelievable! another bug or bad document stuff by apple.

This is what the docs say:

Updating Your Image Resource Files

Applications running in iPhone OS 4 should now include two separate files for each image resource. One file provides a standard-resolution version of a given image, and the second provides a high-resolution version of the same image. The naming conventions for each pair of image files is as follows:

Standard: ImageName device_modifier . filename_extension

High resolution: ImageName @2x device_modifier. filename_extension

The and portions of each name specify the usual name and extension for the file. The portion is optional and contains either the string ~ipad or ~iphone. You include one of these modifiers when you want to specify different versions of an image for iPad and iPhone. The inclusion of the @2x modifier for the high-resolution image is new and lets the system know that the image is the high-resolution variant of the standard image.

So, according to the docs you should name your images as I did: "myImage~iphone.png" (lores), "myImage@2x~iphone.png" (hires) and "myImage~ipad.png" (ipad).

Due to bad documented docs (as usual with their docs) and buggy SDK, this is the reality:

  1. you should name your files as "myImage~iphone.png" (lores), "[email protected]" (hires) and "myImage.png" (ipad).

IN OTHER WORDS: PUT THE @2X AT THE END, not in the middle as recommended by Apple and DON'T PUT ANY TILDE STUFF on the iPad images. But this is not all: even if you do all this, you will only be able to load your images using [UIImage imageNamed:...], that means all your images will be cached. This is because ImageWithContentsOfFile doesn't work on the SDK 4, even being stated on the docs it does.

The complete solution involves using THIS STUFF.

Thanks Apple for this half-cooked stuff.

update to this answer: It shows that I am right after all. This is a bug. I have reported it to apple on July 8 and today I received this message from them: "This is a follow up to Bug ID# 8161396. After further investigation it has been determined that this is a known issue, which is currently being investigated by engineering. This issue has been filed in our bug database under the original Bug ID# 8084451."

Digital Robot
You are wrong about the ordering in the naming convention for high-resolution files. Apple's documentation is correct there. For example, I modified the icon that appears in the lower left of my application to be RotationIcon@2x~iphone.png and RotationIcon~iphone.png, and the high-resolution version loads as expected: http://www.sunsetlakesoftware.com/sites/default/files/Molecules-ResolutionTest.zip . If it is named [email protected], it does not load. You need to make sure that you do not have a RotationIcon.png, or the device type will be ignored when loading.
Brad Larson
What Apple say is not working for me. Try to make an universal app and have 3 images: iphone, iphone 4g and iPad and tell me if it works. The iPad version cannot have the ~ipad or it will not load (because iOS 3.2 will not recognize it) and the @2x has to be at the end (or iphone 4 will load it twice the size).
Digital Robot
As you can see in the project I linked to, the RotationIcon images load at the proper scale if named following Apple's convention, but don't load otherwise (go ahead, try changing the names to your ordering and the high-resolution version will not load). As far as the iPad goes, I believe this transparent loading of different image variants for a named image is new with iOS 4.0, and even the device-specific loading was not present in 3.2, so the iPad cannot make use of this. It is a little misleading in the documentation, but this should be resolved with the next iPad OS update.
Brad Larson
In the meantime, you can simply do a UI_USER_INTERFACE_IDIOM() check and load the ~ipad image version manually if +imageNamed: returns nil.
Brad Larson
Also do not forget Mike to file a documentation or feature request bug with Apple, honestly compared to other documentations Apple's is highly accurate and complete.
valexa
I did it already. :-)
Digital Robot
Hi guys, it shows that I was right. This is a bug. I have reported it to apple on July 8 and today I received this message from them: "This is a follow up to Bug ID# 8161396. After further investigation it has been determined that this is a known issue, which is currently being investigated by engineering. This issue has been filed in our bug database under the original Bug ID# 8084451."
Digital Robot