7

Loading images from URL into a uiimage and then adding those images to a uitableviewcell uiimageview, like below code. I do not know the image sizes, but need to force them to be 40x40 in the tableviewcell image. The images keep loading with different widths in the uitableview.

Read through other posts related to uiimage sizing/scaling, but those solutions don't work for me. I am thinking it's because I am using the uiimageview included in uitableviewcell vs creating 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've tried setting content mode (tried both aspectfill and aspect fit), tried resetting the frame, setting autoresizingmask, clipTobound. none of it changed a thing.

Augie
  • 1,341
  • 1
  • 11
  • 18
  • I just tried this with a custom uiimageview and it works fine. Must be something with the included uimageview in uitableviewcell – Augie Aug 07 '12 at 13:58

3 Answers3

22

Found the answer looking at Apples example project "LazyTableImages"

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;
Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
Augie
  • 1,341
  • 1
  • 11
  • 18
  • I tried, but get an message "you must wait 2 days to accept your own answer". – Augie Aug 07 '12 at 17:32
  • What message of which class do you have to put this in? Is this within your subclass of UITableView or UITableViewCell? – Edd Slipszenko Aug 23 '12 at 13:53
  • 1
    above code was in 'tableView: cellForRowAtIndexPath:' in the main thread, but later I switched my code to mimic the example in 'lazy table images' on apples dev site, which is to call a background thread from 'tableView: cellForRowAtIndexPath:' and then download the image, save it to a uiimage and upon completion, return the uiimage to the main thread to put into the cell.imageView. I think icondownloader was the name of the object running in background in the example project. – Augie Aug 23 '12 at 14:22
  • Thanks, I'll give this a go. The exact problem that I'm having is more precisely described here: http://stackoverflow.com/questions/8085750/uiimageview-in-custom-uitableviewcell-not-respecting-aspect-fit-mode but unfortunately that question hasn't received any answers. – Edd Slipszenko Aug 23 '12 at 14:35
  • I tried this. It worked but the image becomes blurry! I tried also the first answer of following post and it worked better (without making image blurry): http://stackoverflow.com/questions/2788028/how-do-i-make-uitableviewcells-imageview-a-fixed-size-even-when-the-image-is-sm – Sacha Aug 16 '16 at 18:38
1

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

select the UIImage and go to attributes inspector and check "Clip Subviews"

make sure you have constraints set for the image's size.

NSGodMode
  • 599
  • 1
  • 6
  • 16
1

I had this problem when I was mistakenly using the imageView property of the cell rather than the property for the custom UIImageView I created in the storyboard. When I used the proper reference to the custom view, it all worked as expected. I suspect some of you may have done similar.

Litehouse
  • 864
  • 8
  • 14