简体   繁体   中英

UIImageView aspect fill - image outside UIImageView bounds

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.

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.

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:

在此处输入图片说明

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

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

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:

[imageView setClipsToBounds:YES];

Or using interface builder:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM