UIViewContentModeScaleAspectFit iphone sdk gives poor quality image

假装没事ソ 提交于 2019-12-08 03:23:47

问题


Hopefully a quick one? I am creating a custom uitableviewcell and have added an imageview. I have some PNG images which are around 200x200 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 50x50 frame.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/3978211/uiviewcontentmodescaleaspectfit-iphone-sdk-gives-poor-quality-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!