resize – UIViewContentModeScaleAspectFit iphone sdk gives poor quality image – Education Career Blog

Hopefully a quick one? I am creating a custom uitableviewcell and have added an imageview.
I have some PNG images which are around 200×200 in size. I want to create a thumbnail to put in the tableview, but when I resize the image, it results in a poor quality image.

I use UIViewContentModeScaleAspectFit to resize it to a 50×50 frame.

Poor quality image from resize

Should I be calling a better draw resize on each image before I Put it to the table cell? There will be around 20-40 images in each table, so I don’t want to over work the device!!

Thanks for any help.

,

Rescaling the images yourself with CoreGraphics will give you more control over the quality, but your best bet is to size the images appropriately for your table in the first place — less work the software has to do and complete control over the image’s appearance.

If you still want to resize them in Quartz, here’s one way you might go about it:

UIImage* originalThumbnail = UIImage imageWithContentsOfFile:<PATH_TO_IMAGE>;

CGSize originalSize = originalThumbnail size;
CGSize cropSize = { 50, 50 };

CGRect cropRect = CGRectMake(abs(cropSize.width - originalSize.width)/2.0, abs(cropSize.height - originalSize.height)/2.0, cropSize.width, cropSize.height);

CGImageRef imageRef = CGImageCreateWithImageInRect(originalThumbnail CGImage, cropRect);

// here's your cropped UIImage
UIImage* croppedThumbnail = UIImage imageWithCGImage:imageRef;  
CGImageRelease(imageRef);

You’d want to do this once if possible, i.e. not every time you construct your UITableViewCell.

Leave a Comment