Set UIImageView Size in UITableViewCell when UIImage loaded from URL?

前端 未结 3 2053
天命终不由人
天命终不由人 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: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;
    

提交回复
热议问题