Set UIImageView Size to UITableViewCell when UIImage is loaded from URL?

Downloading images from a URL in uiimage and then adding these images to uijimageview uitableviewcell, as shown below. I do not know the image sizes, but you need to make them be 40x40 in the tableviewcell image. Images save downloads with different widths in a uitableview.

Read other posts related to uiimage size / scaling, but these solutions do not work for me. I think because I use the uiimageview included in the uitableviewcell and create my own uiimageview.

Here's the code>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] init]; ItemForSale *item = [listOfItems objectAtIndex:indexPath.row]; cell.textLabel.text = item.name; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.font = [UIFont systemFontOfSize:14]; NSURL *url = [NSURL URLWithString: item.imgPath]; UIImage *thumbnail = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; if (thumbnail == nil) { thumbnail = [UIImage imageNamed:@"noimage.png"] ; } cell.imageView.image = thumbnail; cell.imageView.contentMode = UIViewContentModeScaleAspectFill; cell.imageView.frame = CGRectMake(0, 0, 40, 40); cell.imageView.autoresizingMask = UIViewAutoresizingNone; cell.imageView.clipsToBounds = YES; return cell; } 

I tried setting the content mode (I tried both options aspect and aspect), tried to reset the frame, installed autoresizingmask, clipTobound. none of this changed anything.

+7
source share
2 answers

Found an answer by looking at an example of the LazyTableImages project in Apple

 NSURL *url = [NSURL URLWithString: item.imgPath]; UIImage *thumbnail = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; if (thumbnail == nil) { thumbnail = [UIImage imageNamed:@"noimage.png"] ; } CGSize itemSize = CGSizeMake(40, 40); UIGraphicsBeginImageContext(itemSize); CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); [thumbnail drawInRect:imageRect]; cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return cell; 
+22
source

I had the same problem and it worked perfectly for me;

select UIImage and go to the attribute inspector and check the box "Moving clips"

make sure you have image size limits.

+1
source

All Articles