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