iOS:UIImageView随着UITableView.rowHeight的变化而变化,如何避免? [英] iOS: UIImageView changes with change in UITableView.rowHeight, how to avoid?

查看:258
本文介绍了iOS:UIImageView随着UITableView.rowHeight的变化而变化,如何避免?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 n00b 问题,我在尝试构建应用时正在学习 iOS

This is really a n00b question, I am learning iOS while trying to build an app

我需要在 UITableViewCell 上显示图片,标签。以下代码为我做了这个

I need to show an image, label on UITableViewCell. The following code does that for me

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.imageView.image = [self getImageNameForRow:indexPath.row];
    cell.textLabel.text = self.features[(NSUInteger) indexPath.row];
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

    return cell;
}

问题在于图像尺寸比我预期的要大。所以我试图增加行的高度

The problem is that image size comes bigger that I expect. so I tried to increase the height of row as

self.tableView.rowHeight = 80;

然后图像也会向上扩展。

But then the image also scales up.

如何在增加(或改变)行的大小时保持图像大小不变?

How can I keep my image size fixed while increasing (or changing) the size of the row?

推荐答案

问题是您使用的是默认的表格视图单元格样式。该样式带有内置的 textLabel imageView ,后者是带有约束的UIImageView,因此它调整大小以填充单元格的高度。但你也说过

The problem is that you are using a default table view cell style. That style comes with a built-in textLabel and an imageView, and the latter is a UIImageView with constraints so that it is resized to fill the height of the cell. But you have also said

 cell.imageView.contentMode = UIViewContentModeScaleAspectFit

这意味着随着图像视图的增长,图像随之增长 - 正是您所看到的。

Which means that as the image view grows, the image grows with it - exactly what you are seeing.

我解释此处的解决方案是将图像缩小到您想要它的实际大小 - 并将图像视图的contentMode设置为 center 。这样的事情:

The solution, as I explain here, is to size the image down to the actual size that you want it - and set the image view's contentMode to center. Something like this:

UIImage* im = [self getImageNameForRow:indexPath.row];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36,36), YES, 0);
[im drawInRect:CGRectMake(0,0,36,36)];
UIImage* im2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = im2;
cell.imageView.contentMode = UIViewContentModeCenter;

36,36 改为大小你真的想要。

Change that 36,36 to the size you actually want.

无论如何,这是一个好习惯。以比实际显示所需的更大的尺寸保持图像是一种可怕的记忆浪费(浪费的存储量以指数方式增长,因为面积在单个维度的平方的量级上)。所以你应该始终将图像大小缩小到实际的显示尺寸。 Stack Overflow上有很多代码可以显示许多其他方法。

This is good practice anyway. It is a terrible waste of memory to hold onto an image at a larger size than needed for actual display (the amount of wasted memory grows exponentially, because area is on the order of the square of a single dimension). So you should always size images down to their actual display size. There's lots and lots of code on Stack Overflow showing many other ways to do that.

这篇关于iOS:UIImageView随着UITableView.rowHeight的变化而变化,如何避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆