UIImageView方面填充-在UIImageView边界外的图像

时间:2023-02-09 17:54:59

I have a UITableView with cells that contain images. I want to download a big image from the server and insert it into my cell.

我有一个带有图像的单元格的UITableView。我想从服务器上下载一个大图并将它插入到我的单元格中。

My UITableViewCell has a fixed height and fills the entire width of the screen. The best solution will be to resize the image proportionally to fit the screen width and then crop the area with the fixed height, but I found that resizing a UIImage is really hard, and I don't understand why. If you can help me with this, it will be very good.

我的UITableViewCell具有固定的高度,并填充屏幕的整个宽度。最好的解决方案是按比例调整图像大小以适应屏幕宽度,然后用固定的高度裁剪区域,但是我发现调整UIImage的大小真的很难,我不明白为什么。如果你能帮助我,那就太好了。

Anyway, at the moment, I am trying to use the AspectFill mode (I already tried AspectFit and ScaleToFill and it is not what I am looking for) for my UIImageView in UITableViewCell, but for some reason, my image is higher than the cell height:

无论如何,目前,我正在尝试使用AspectFill模式(我已经尝试了AspectFit和ScaleToFill,它不是我想要的)在UITableViewCell中的UIImageView,但是出于某种原因,我的图像高于单元格的高度:

UIImageView方面填充-在UIImageView边界外的图像

I don't understand what I am doing wrong. It is not auto layout, it is fixed height. Why didn't it work?

我不明白我做错了什么。它不是自动布局,而是固定高度。它为什么不工作?

2 个解决方案

#1


18  

Make sure to set the clipsToBounds property of the UIImageView to YES (or true in Swift).

确保将UIImageView的clipsToBounds属性设置为YES(或Swift中的true)。

#2


5  

It looks like your image view is the right size, but UIImageView does not automatically clip an image. You need to set your UIImageView's clipsToBounds property to YES, you can do this in code:

看起来你的图像视图的大小是正确的,但是UIImageView不会自动剪辑图像。你需要设置你的UIImageView的clipsToBounds属性为YES,你可以在代码中这样做:

[imageView setClipsToBounds:YES];

Or using interface builder:

或者使用界面构建器:

UIImageView方面填充-在UIImageView边界外的图像

#1


18  

Make sure to set the clipsToBounds property of the UIImageView to YES (or true in Swift).

确保将UIImageView的clipsToBounds属性设置为YES(或Swift中的true)。

#2


5  

It looks like your image view is the right size, but UIImageView does not automatically clip an image. You need to set your UIImageView's clipsToBounds property to YES, you can do this in code:

看起来你的图像视图的大小是正确的,但是UIImageView不会自动剪辑图像。你需要设置你的UIImageView的clipsToBounds属性为YES,你可以在代码中这样做:

[imageView setClipsToBounds:YES];

Or using interface builder:

或者使用界面构建器:

UIImageView方面填充-在UIImageView边界外的图像