gpt4 book ai didi

iOS - UITableViewCell 图像调整/调整大小

转载 作者:技术小花猫 更新时间:2023-10-29 10:22:43 25 4
gpt4 key购买 nike

我已经创建了一个 TableView 并用文本填充了它的单元格。我想做的是向单元格添加图像,但发生的事情是图像覆盖了整个单元格,看起来很糟糕......

这就是我正在做的:cell.imageView.image = [UIImage imageNamed:@"test2.jpeg"];

这就是它的样子:enter image description here

我想让它看起来像:enter image description here

如何调整表格 View 单元格的图像大小以获得所需的结果?

更新

这是我在以下答案中尝试建议的内容:

cell.imageView.frame = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
cell.imageView.layer.cornerRadius = 8.0;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.layer.masksToBounds = YES;
cell.imageView.image = [UIImage imageNamed:@"test2.jpeg"];

但我得到的输出仍然是:

enter image description here

更新 2.0

我尝试了一些建议的其他方法:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 30, 30)];
imageView.backgroundColor = [UIColor clearColor];
[imageView.layer setCornerRadius:8.0f];
[imageView.layer setMasksToBounds:YES];
[imageView setImage:[UIImage imageNamed:@"test2.jpeg"]];
[cell.contentView addSubview:imageView];

但现在我明白了: enter image description here

最佳答案

首先,您需要将单元格的 imageView 设置为您想要的最大尺寸。然后通过 Interface Builder 或代码确保您拥有正确的内容模式:

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

基本上在这种模式下,它会尝试适应您为图像创建的空间,但它会保持纵横比。

Jackson发布了一张显示内容模式的好图片 here :

enter image description here

对于角落,您将需要 QuartzCore,您可以使用以下代码进行设置:

#import <QuartzCore/QuartzCore.h>

...

cell.imageView.layer.cornerRadius = 4;
cell.imageView.layer.masksToBounds = YES;

更新

如果您的 imageView 不是 socket ,您最好自己将一个 imageView 添加到单元格中。你可以(而且你应该)通过 Storyboard/xib 来做到这一点。 This tutorial might help you关于这一点,尽管互联网上有很多。 This stackoverflow question给出了该问题的其他解决方案。

关于iOS - UITableViewCell 图像调整/调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20861204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com