• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - TableView 中图像的大小不正确

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:25:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用来自 URL 的图像创建一个表格 View ,但是在我将其按在行中之前,图像不会重新调整到所有 View 的大小。 知道为什么会这样吗? 这是一个自定义的 tableview

我的代码是:

- (UITableViewCell *)tableViewUITableView *)tableView    cellForRowAtIndexPathNSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Celula_Noticias";

   Celula_Noticias *cell = (Celula_Noticias*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

    NSArray * top= [[NSBundle mainBundle] loadNibNamed"Celula_Noticias" owner:self options:nil];

    for(id currentObject in top){
        if ([currentObject isKindOfClass:[Celula_Noticias class]]) {
            cell= (Celula_Noticias *) currentObject;
            break;
        }

    }


}


cell.Image_Noticias_1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[noticias objectAtIndex:indexPath.row ] objectAtIndex:0]]]];

cell.Image_Noticias_2.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[noticias objectAtIndex:indexPath.row ] objectAtIndex:2]]]];

cell.Titulo_Noticias_1.text=[[noticias objectAtIndex:indexPath.row ] objectAtIndex:1];
cell.Titulo_Noticias_2.text=[[noticias objectAtIndex:indexPath.row ] objectAtIndex:3];


return cell;

}

表格已正确填充,但图像的开头尺寸不正确。

我尝试过使用 CGRectMake,但还是不行。

谢谢。



Best Answer-推荐答案


创建一个函数对下载的图像进行后期处理:

- (UIImage*)postProcessImageUIImage*)image
{
        CGSize itemSize = CGSizeMake(50, 50);

        UIGraphicsBeginImageContext(itemSize);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [image drawInRect:imageRect];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
}

上面的函数会给你一个50*50的漂亮图像。

那么你可以这样做:

UIImage* downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString"...url goes here..."]]];
UIImage* newImage = [self posProcessImage:downloadedImage];
cell.imageView.image=newImage

关于iphone - TableView 中图像的大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6178579/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap