简体   繁体   中英

Cut image to fit in image view

I have a UIImageView with frame 320x240.

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
imgView.image = [self getImageFromServer:100];

I have tried many ways bit no one resulted for me.

The app loads images from server, but some of them are 320x320.

What i must to cut the image received? It's to cut, does'n mater if someone is going to appear without head.

Regards

imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.clipsToBounds = true;

You can use the contentMode as Center if you want to keep always the size or you can resize untill it fits and then crop using the Scale Aspect Fit contend mode. You also need to clip the subviews.

You can do both actions via Interface Builder:

Setting the content mode:

在此处输入图片说明

Setting clip SubViews to YES:

在此处输入图片说明

Or programmatically:

//set the contentMode
imgView.contentMode = .contentMode = UIViewContentModeCenter;

//set clip subviews
test.clipsToBounds = YES;

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