简体   繁体   English

剪切图像以适合图像视图

[英]Cut image to fit in image view

I have a UIImageView with frame 320x240. 我有一个UIImageView,框架为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. 该应用程序从服务器加载图像,但其中一些是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. 如果要始终保持大小,则可以将contentMode用作“ Center ,也可以将其调整为合适的大小,然后使用“ Scale Aspect Fit竞争模式进行裁剪。 You also need to clip the subviews. 您还需要剪切子视图。

You can do both actions via Interface Builder: 您可以通过Interface Builder进行这两种操作:

Setting the content mode: 设置内容模式:

在此处输入图片说明

Setting clip SubViews to YES: 将剪辑SubViews设置为YES:

在此处输入图片说明

Or programmatically: 或以编程方式:

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

//set clip subviews
test.clipsToBounds = YES;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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