Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS : Reduce image size without reducing image quality

I am displaying an image in tableview cell (Image name saved in a plist). Before setting it to the cell, I am resizing the image to
imageSize = CGSizeMake(32, 32);

But, after resizing the image, quality is also getting degraded in retina display.

enter image description here

I have both the images added to the project (i.e. 1x and @2x).

This is how I am reducing the image size to 32x32.

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}

Any pointers on this is very much appreciated.

Thanks

like image 393
A.S. Avatar asked Apr 01 '12 22:04

A.S.


1 Answers

try this : instead of UIGraphicsBeginImageContext(size);use UIGraphicsBeginImageContextWithOptions(size,NO,0.0);

from what i understand what you're doing there is resizing the image to 32x32 (in points) no matter what the resolution. the UIGraphicsBeginImageContextWithOptions scales the image to the scale of the device's screen..so you have the image resized to 32x32 points but the resolution is kept for retina display

(note that this is what i understood from apple's uikit reference..it may not be so..but it should) read here

like image 62
skytz Avatar answered Oct 20 '22 15:10

skytz