2

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.

James White
  • 774
  • 1
  • 6
  • 18

2 Answers2

0

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.

elp
  • 8,021
  • 7
  • 61
  • 120
  • Check this thread: http://stackoverflow.com/questions/3294100/how-to-differentiate-between-iphone4-and-iphone-3 to see how you can check if the device has a retina screen. – rckoenes Jun 28 '11 at 08:14
  • Hi Paska. Thanks, but I think you've misunderstood me. I'm not trying to automate anything. I have two images for each, on the server, and I have some code elsewhere that access both of these and saves them to the app's document directory. This is all working fine, and the images load up into the table cells, but in the case of the @2x images, they appear shrunken. – James White Jun 28 '11 at 08:17
  • Thanks also @rckoenes. Yes, I read that thread. But this is also not my problem. The phone is displaying the correct image (the larger image on a retina screen, otherwise the smaller). But when it's the larger image, the image is shrunk down so that it only fills half of the size of the cell's imageView. – James White Jun 28 '11 at 08:29
0

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.

marcus
  • 2,521
  • 1
  • 23
  • 32