Set UIImageView Size in UITableViewCell when UIImage loaded from URL?

前端 未结 3 2051
天命终不由人
天命终不由人 2021-02-03 13:32

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

相关标签:
3条回答
  • 2021-02-03 14:12

    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.

    0 讨论(0)
  • 2021-02-03 14:23

    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.

    0 讨论(0)
  • 2021-02-03 14:37

    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;
    
    0 讨论(0)
提交回复
热议问题