简体   繁体   中英

iphone: Scale the image in table cell

I am using a UITableViewCellStyleSubTitle. I am trying to add thumbnails to my table cells. UIViewContentModeScaleAspectFit does not seem to work for reasons unknown. I have seen a lot of answers in stack overflow for similar kind of questions. If i crop the image using drawInRect it works, like this

[thumbnail drawInRect:thumbnailRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

But what i am trying to achieve should not need me to do this. I am only trying to scale the image and not crop it. So setting the content mode to "UIViewContentModeScaleAspectFit" for the imageview of the default cell should work.. but it does not.

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

Complete logic for setting thumbnails.. fPath is the path to image

UIImage *thumbnail = [UIImage imageWithContentsOfFile:fPath];
[cell.imageView setImage:thumbnail];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.clipsToBounds = YES;

I found out that drawInRect stretches the original image if the size is lower or croppes it if it is bigger.

This article will help you to prepare a thumbnail of your image http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

It has several useful UIImage classes extentions: * UIImage+Resize * UIImage+Alpha * UIImage+RoundedCorner

Example Usage:

UIImage *imageThumb = [YourImage thumbnailImage:70 transparentBorder:0 cornerRadius:10 interpolationQuality:kCGInterpolationHigh];

and then you can use drawInRect method. If the image is not rectangle - the image will crop the bigger side and take the middle of the image

Hope this answer will guide you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM