Ios – UIViewContentModeScaleAspectFill not clipping

iosuiimageview

I'm trying to draw some thumbnail images at a fixed size (100×100) using UIImageView. I've set the frame size of my image view to be 100×100, and set the contentMode to UIViewContentModeScaleAspectFill.

My understanding is that this should cause the image to be drawn at the size I set, and the image will fill the area of the image, and clip any portions of the image that are outside the size I set for the image view. However, the image is still drawn larger or smaller than the 100×100 size that I set for it.

If I set the contentMode to UIviewContentModeScaleToFill, then the image is drawn at exactly 100×100, but it's distorted to fit into those dimensions. Any ideas why the aspect fill isn't clipping as expected?

Here's my code:

_photoView = [[UIImageView alloc] initWithImage:photoImage];
_photoView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:_photoView];

CGRect photoFrame = _photoView.frame;
photoFrame.size = CGSizeMake(100, 100);
_photoView.frame = photoFrame;

To better illustrate, here's an screenshot of what I'm seeing. The green squares are the UIView containing the UIImageView I'm working with. I've set their background color to green, and the frame of the view to 100×100. As a test, the UIImageViews are sized to 50×50. As you can see, when using the ...AspectFill, the images aren't sized to 50×50, and in some cases are offset from where I'd like them. When using ...ScaleToFill, they're sized correctly, but the image is distorted.

Screenshot illustrating problem

Best Answer

Can you try setting clip to bounds

[_photoview setClipsToBounds:YES];

Or directly in your Storyboard / Xib if you can :

enter image description here

Related Topic