harinsa
harinsa

Reputation: 3196

iOS image size of retina image from web service

Suppose I want an image to be 320x100 points for a retina screen, I would have to make an image 640x200 pixel and named it @2x. The problem is when I download an image from a web service of size 640x200 pixel. Normally a @2x image would be translate to size 320x100 points in the phone, but the image from the web service is still 640x200 points.

Note - the web service is my own, so I can fix it if it's the web service's problem.

Sorry If might have not worded the problem well, but this is what I meant(similar): Retina display and [UIImage initWithData]

Upvotes: 0

Views: 602

Answers (2)

Monstieur
Monstieur

Reputation: 8102

You'll have to set the height and width of the control in points manually and it will automatically display the image with a higher DPI without unnecesarily downscaling.

Upvotes: 1

David Doyle
David Doyle

Reputation: 1716

An @2x image will always be twice the dimensions of a non-retina image if it wasn't loaded through imageNamed.

Create an image like so on a retina and non-retina device:

UIImage *anImage = [UIImage imageNamed: @"anImageName"];
NSLog(@"%@: scale: %f", NSStringFromCGSize(anImage.size), anImage.scale);

The CGSize object printed on the retina device will be the same size of the non-retina image, but will have the scale set to 2.0.

Creating an image using the explicit retina suffix will reveal that the image is actually twice as big - so imageNamed does its own image scaling.

For the case where you want to display this in an already created and sized image view, you still don't need to do anything - just load it straight in and the image view will adjust the image to the correct size.

If, however, you want to create a new image view, then you'll need to create a frame paying attention to the UIScreen's resolution like so (unfortunately, you can't just set the scale property as its read only):

CGRect newFrame = CGRectZero;
newFrame.size.width = (anImage.size.width / [UIScreen mainScreen].scale);
newFrame.size.height = (anImage.size.height / [UIScreen mainScreen].scale);

This assumes that your web service is aware of wether the screen of the device is retina or not; some services will pick this up automatically, some will require you to tell them this up front. YMMV.

Upvotes: 0

Related Questions