resizing imageview images in IOS UITableViewCell

Source: Internet
Author: User

In my project, many places want to UITableViewCell in the ImageView can be based on the size of their pictures to show, and in order to solve this problem and feel that rewrite UITableViewCell is not worth it.

As follows:

:

In fact, I only picture deformation problem. So the first time I think about it is to adjust the ImageView content layout mode and the size of the outer frame

Cell.imageview.contentmode=uiviewcontentmodecenter;

Cell.imageview.frame= ....

In addition, such as:

cell.imageView.autoresizingMask = ( UIViewAutoresizingNone );cell.imageView.autoresizesSubviews = NO;cell.imageView.contentMode = UIViewContentModeCenter;cell.imageView.bounds = CGRectMake(0, 0, 50, 50);cell.imageView.frame = CGRectMake(0, 0, 50, 50);cell.imageView.image = [UIImage imageWithData: imageData];

But these methods do not work in UITableViewCell ....

Found on the Internet long time also did not find, later or in the StackOverflow found a kind of their own preferred solution, as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [...]        
//1、首先对image付值
cell.imageView.image=[UIImage imageNamed:@"灰时间"];
//2、调整大小
      CGSize itemSize = CGSizeMake(40, 40);      UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);      CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);      [cell.imageView.image drawInRect:imageRect];      cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();      UIGraphicsEndImageContext(); [...]     return cell;}

The results are as follows:

Very happy ... Perfect solution

resizing imageview images in IOS UITableViewCell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.