iOS - UITableViewCell图像调整/调整大小 [英] iOS - UITableViewCell image adjust/resize

查看:132
本文介绍了iOS - UITableViewCell图像调整/调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表视图,并用文本填充其单元格。我想要做的是向细胞添加一个图像,但是发生的事情是图像在整个细胞中徘徊而且它看起来很糟糕......



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



这就是它的样子:



我想让它看起来像是:



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



更新



这是我在下面的答案中尝试过的建议:

  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];

但我得到的输出仍然是:





更新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];

但现在我得到了这个:

解决方案

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

  cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 

基本上在此模式下,它会尝试适合您为图像创建的空间,但它会保持宽高比。



,尽管有很多他们在互联网上。 此stackoverflow问题为该问题提供了其他解决方案。


I have create a table view and have filled its cells with text. What I'm trying to do is add an image to the cell but whats happening is the image is coving the whole cell and it looks bad...

This is what I'm doing: cell.imageView.image = [UIImage imageNamed:@"test2.jpeg"];

And this is what it looks like:

What I'm trying to get it to look like is:

How do i resize the image of the table view cell in order to get the desired result?

Update

This is what I have tried suggested in the answers below:

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"];

but still the output I'm getting is:

Update 2.0

Ive tried some other ways suggested:

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];

but now I'm getting this:

解决方案

First you need to set the cell's imageView to the max size you want. Then assure via Interface Builder or via code that you have the correct content mode:

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

Basically with this mode it will try to fit the space you created for your image but it will maintain the aspect ratio.

Jackson posted a good image that shows the content modes here:

For the corner you will need QuartzCore and you can do set it with this code:

#import <QuartzCore/QuartzCore.h>

...

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

UPDATE

If your imageView isn't an outlet you are better off adding one imageView yourself to the cell. You can (and you should) do that via storyboard/xib. This tutorial might help you on that, although there are a lot of them on the internet. This stackoverflow question gives other solutions to that problem.

这篇关于iOS - UITableViewCell图像调整/调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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