After reading Apple's own HIG (specifically the 'Creating images for retina..' section) and looking at similar answers and suggested blogs - I can't seem to get a good solid fix on how properly design images for retina.
To best explain, let me set up a scenario:
I have a UIButton that is 44 x 44 points that I laid out in IB.
I go to photoshop, create an image that is 44 x 44 pixels. I save that as image#2x.png
I save another, without the #2x.png appended on the file name.
In the code, I do something like ... [UIImage imageNamed:#"image"]
From what I have read, it seems that DPI doesn't matter. I do also understand that retina images are, of course, double in scale to the original image. So for the scenario above, was the image#2x.png supposed to be 88 x 88 pixels instead of 44 x 44 pixels? In the [UIImage imageNamed:#"image"], do I need to specify the #2x in the image's name, or does xcode take care of that?
Going with the scenario I described above, can someone either correct me or confirm that this is the correct pixel dimensions the image?
The scenario you described is wrong.
Here are the right:
Assume you want a 44pt x 44pt button to place in IB;
Go to Photoshop create a 88px x 88px image named with #2x;
Create another image that 44px x 44px without addition #2x;
In your IB or code, set the image use normal name, which without #2x
in the image name. System will display the normal image and #2x
image properly.
To create button which IB size is 44x44 you need two images:
Image.png which size is 44x44
Image#2x.png which size is 88x88
iOS will automatically choose proper file when you use the base name
To load that image in code you simply use line below:
[UIImage imageNamed:#"Image"]
and again, iOS will get proper image
Related
I have a table where the cells' image views are being populated by images that have been previously pulled down off a server. So:
[[cell imageView] setImage:[[[UIImage alloc] initWithContentsOfFile:filePath] autorelease]];
Where "filePath" is the location of these images. Working beautifully, until I decided to be clever and add retina display images to my server. These images (double-sized, obviously) are being displayed, but are shrunk. I had labelled them image#2x.png, hoping that the iPhone would just know what to do with them, but obviously that doesn't work in this context.
I've looked at the discussions, and am guessing I need to do something with the contentsScale of the cell's imageView, like matching it to the screen scale, but I'm not sure exactly how to do this. Any help appreciated.
From server you cannot download automatically retina images.
You need a control like
if (iphone == 4) image=img#2x.png
else image=img.png
to get correct images.
You would need to set the scale factor of the image correctly. Please check the scale property in UIImage
If you load an image from a file whose
name includes the #2x modifier, the
scale is set to 2.0. If the filename
does not include the modifier but is
in the PNG or JPEG format and has an
associated DPI value, a corresponding
scale factor is computed and reflected
in this property. You can also specify
an explicit scale factor when
initializing an image from a Core
Graphics image. All other images are
assumed to have a scale factor of 1.0.
So you can read your image as above, get the CGImage from it and create a new UIImage using + (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation.
A nicer way could be to check the DPI of your retina image and make it 144dpi with a graphics program. According to the scale property documentation this might work, too.
since iPhone 4 now has 640X960 resolution i am confused how to build a proper UI for both 320 X 480 and 640 X 960 resolutions.. if i only use images and stuff of 640X960 resolution will it automatically adjust itself for smaller resolution iPhones.. if not what is the best way to differentiate on basis of device resolution??
You make 2 of each image - the first one at normal scale and named the usual "whatever.png". Then you make a second hi-res version of that image at double the scale and name it "whatever#2x.png". Then, iOS will handle the rest. You'll add both of those images to the project, and anywhere in the code that you reference "whatever.png", the system will take care of grabbing and using "whatever#2x.png" when and if appropriate, so you don't have to fuss with much. That's it. Simple.
In Interface Builder, just lay things out like normal using the standard sized graphics. In code as well, just reference images as usual, referring to the standard-sized images. So long as the higher-res counterpart is named with the "#2x.png" appended, everything will just work.
So, just as an example, say you currently have a project with a button image sized 40x100 named "awesomeButton.png". To support the retina resolution, create a new button image sized at 80x200, and name it awesomeButton#2x.png. Import it into your project and iOS takes care of the rest.
I've got some image generating code that uses UIGraphicsBeginImageContext(), UIGraphicsGetImageFromCurrentImageContext() and UIImagePNGRepresentation() to do some drawing, then save it to disk as a PNG for later use.
Does UIImagePNGRepresentation() take into account scale? As in, if I have an image that is 20 points wide, will the resultant PNG be 20 pixels or 40 pixels?
Also, when I display these images, I use [UIImage imageWithContentsOfFile:] and [image drawInRect:]. Is there a way to hint to those methods to use higher resolution drawing?
Per the iPhone Application Programming Guide you should use UIGraphicsBeginImageContextWithOptions with a scale of 2.0 to create a properly scaled context for the iPhone 4.
As for the second part of your question, I believe you should save the resulting png with the #2x suffix on the base name (e.g., myImage#2x.png). Then when you load it back in using UIImage, its size and scale will be correctly set. Otherwise your image will be loaded at scale 1.0 and be twice as large (in points) as you expect it to be. This section of the same document goes into a bit of detail regarding high-resolution images for devices with high-resolution displays.
I have a button with an image set through interface builder. The original image is SearchImage.png and the high rez version is SearchImage#2x.png. I'm absolutely sure that no typos were made, and the higher resolution image is indeed exactly twice the size (ie twice as tall, twice as wide) as the lower resolution image, yet the office's iPhone4 still only loads the low resolution image.
Does anyone have any ideas what the problem might be?
I have read all the relevant Apple documentation.
Thanks!
Tristan
Just assign the image property "SearchImage.png" and include both SearchImage.png and SearchImage#2x.png in your main bundle and it will load the correct image.
See https://devforums.apple.com/message/233916#233916.
I have 6 images, all the same size (65x65). When I create an imageview and add this image through the "image view attributes" in the interface builder, the picture is very small. Maybe 5x5. The pictures also show up small when viewing them in the resource folder however, when running the program, they are the right size. Is there some setting I need to change to get the images to show the full size/full resolution within the interface builder?
Thanks.
Make sure your UIImageView is active, then go to Layout -> Size to Fit (Command + '=' should also do the trick). This way the UIImageView will resize to fit the image and it'll look nice when looking at your stuff in IB.
Adjust the resolution of your image files to 72 dpi.