ios 如何管理 UIImageView 内容模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14134035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:43:04  来源:igfitidea点击:

How to manage UIImageView content mode?

iphoneiosipaduiimageview

提问by DipakSonara

I have an UIImageView with some fixed rect size. But my images are not fixed in size. I want to display images according to UIImageView's rect size. Whether image is big or large in resolution it must be in fixed display according to UIImageView's size. I am confused in using below assets.

我有一个固定矩形大小的 UIImageView。但是我的图像大小不固定。我想根据 UIImageView 的矩形大小显示图像。无论图像是大还是大,都必须根据 UIImageView 的大小固定显示。我对使用以下资产感到困惑。

UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw

What to set in autoresize mask ? How do they behave ?

在自动调整大小掩码中设置什么?他们表现如何?

回答by sagarcool89

Please try this code, Hope it will work for you.

请试试这个代码,希望它对你有用。

set UIImageViewcontentModeto UIViewContentModeScaleAspectFillas below :

设置UIImageViewcontentModeUIViewContentModeScaleAspectFill如下:

imageView.contentMode = UIViewContentModeScaleAspectFill;

set autoresizingMask of UIImageView as below :

设置 UIImageView 的 autoresizingMask 如下:

imageView.autoresizingMask =   
    ( UIViewAutoresizingFlexibleBottomMargin
    | UIViewAutoresizingFlexibleHeight
    | UIViewAutoresizingFlexibleLeftMargin
    | UIViewAutoresizingFlexibleRightMargin
    | UIViewAutoresizingFlexibleTopMargin
    | UIViewAutoresizingFlexibleWidth );

回答by atulkhatri

You just need these two lines:

你只需要这两行:

imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.layer.clipsToBounds = YES; // Must do this or else image will overflow

回答by Ali Raza

In swift you set content mode as well

在 swift 中,您也可以设置内容模式

var newImgThumb : UIImageView
newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
newImgThumb.contentMode = .ScaleAspectFit

回答by Urkman

Here a solution for swift:

这里有一个快速的解决方案:

let imageView = UIImageView(image: UIImage(named: "backgroundImage"))
imageView.frame = tableView.frame
imageView.contentMode = .ScaleAspectFill
tableView.backgroundView = imageView

回答by kickinbahk

Worth noting that swift 3 has changed as following

值得注意的是,swift 3 已更改如下

.ScaleAspectFitto scaleAspectFit

. ScaleAspectFitscaleAspectFit

.ScaleAspectFillto .scaleAspectFill

. ScaleAspectFill到 。scaleAspectFill

回答by NiravPatel

If you want to make your UIImageViewsize according to ratio ,then you must set it's mode to UIViewContentModeScaleAspectFit

如果你想UIImageView根据比例制作你的尺寸,那么你必须将它的模式设置为UIViewContentModeScaleAspectFit

 yourimage.contentMode=UIViewContentModeScaleAspectFit;

let me know it is working or not!!!

让我知道它是否有效!!!

Happy Coding.

快乐编码。

回答by SachinVsSachin

Hope this helpful for you

希望这对你有帮助

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);

image = [UIImage imageWithCGImage:imageRef]];

CGImageRelease(imageRef);