代码之家  ›  专栏  ›  技术社区  ›  stefanB

如何调整图像大小以适应UITableView单元格?

  •  6
  • stefanB  · 技术社区  · 16 年前

    如何配合 UIImage 进入的单元格 UITableView ,uiTableViewCell(?).

    你…吗 addSubview cell 或者有调整大小的方法吗 cell.image 紫外图像 在分配给 细胞图像 ?

    我想保留单元格大小的默认值(无论初始化时是什么样的零矩形),并想在每个条目中添加类似图标的图片。图像略大于单元格大小(表格行大小)。

    我想代码看起来像这样(从我的头上看):

    UIImage * image = [[UIImage alloc] imageWithName:@"bart.jpg"];
    cell = ... dequeue cell in UITableView data source (UITableViewController ...)
    cell.text = @"bart";
    cell.image = image;
    

    要调整图像大小以适应单元格大小,需要做什么? 我见过这样的东西:

    UIImageView * iview = [[UIImageView alloc] initWithImage:image];
    iview.frame = CGRectMake(...); // or something similar
    [cell.contentView addSubview:iview]
    

    上面将添加图像到单元格,我可以计算大小以适合它,但是:

    1. 我不确定是否有更好的 对了,是不是太贵了 添加 UIImageView 只是为了调整 细胞图像 ?
    2. 现在我的标签( cell.text )需要 随着图像的模糊而移动, 我看到了一个解决方案 将文本添加为标签:

    例子:

    UILabel * text = [[UILable alloc] init];
    text.text = @"bart";
    [cell.contentView addSubview:iview];
    [cell.contentView addSubview:label];
    // not sure if this will position the text next to the label
    // edited original example had [cell addSubview:label], maybe that's the problem
    

    有人能指点我正确的方向吗?

    编辑 DoH [cell.contentview addSubview:view] [cell addSubview:view] 也许我应该看看这个:

    - (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = ...;
        CGRect frame = cell.contentView.bounds;
        UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
        myLabel.text = ...;
        [cell.contentView addSubview:myLabel];
        [myLabel release];
    }
    
    2 回复  |  直到 9 年前
        1
  •  2
  •   Alex Reynolds    16 年前

    This site posts code 关于重新缩放A UIImage* . 您可以使用缩放比例来获得 UIIMAGE * 你在用,把它缩小。

        2
  •  1
  •   Robert MouzmiSadiq    15 年前

    如果您可以访问Apple Devloper Center,这里有一个关于为表视图缩放图像的视频。它更关注性能问题,并讨论了很多线程,但他确实展示了一些用于调整图像大小的示例代码。

    视频名称:“有效的iPhone应用程序开发-第2部分”大约30分钟。