Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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

like image 290
Panich Maxim Avatar asked Aug 28 '15 15:08

Panich Maxim


2 Answers

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

like image 165
Daehn Avatar answered Nov 12 '22 22:11

Daehn


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:

enter image description here

like image 6
Ben Kelly Avatar answered Nov 12 '22 23:11

Ben Kelly