ios 如何阻止图像在 UIImageView 中拉伸?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7035523/
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:04:25  来源:igfitidea点击:

How to stop an image from stretching within a UIImageView?

ioscocoa-touchuiimageviewuiimagestretching

提问by R. Dewi

I have a UIImageViewwhere I have set the frame size to x = 0, y = 0, width = 404, height = 712. In my project, I need to change the image in UIImageViewdynamically.

我有一个UIImageView将帧大小设置为x = 0, y = 0, width = 404, height = 712. 在我的项目中,我需要UIImageView动态更改图像。

I am using this code to change the image:

我正在使用此代码来更改图像:

self.imageView.image = [UIImage imageNamed:@"setting-image.png"];

The problem is, when the *.pngimage size is smaller than the UIImageViewframe size, the image stretches. I don't want it to stretch. Is there any way to do that?

问题是,当*.png图像尺寸小于UIImageView帧尺寸时,图像会拉伸。我不希望它伸展。有没有办法做到这一点?

回答by jtbandes

You can use

您可以使用

self.imageView.contentMode = UIViewContentModeScaleAspectFit;

Swift 3:

斯威夫特 3:

imageView.contentMode = .scaleAspectFit

Or UIViewContentModeCenter/ .center, or any of the other modes described in the UIView documentation.

UIViewContentModeCenter/ .center,或UIView 文档中描述的任何其他模式。

回答by Tyler Kidd

Setting clipsToBounds in combination with UIViewContentModeScaleAspectFit contentMode was what did the trick for me. Hope that helps someone!

将 clipsToBounds 与 UIViewContentModeScaleAspectFit contentMode 结合设置对我有用。希望对某人有所帮助!

imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;

回答by Anomie

Use the contentModeproperty. You probably want either UIViewContentModeScaleAspectFitor UIViewContentModeCenter.

使用该contentMode属性。您可能想要UIViewContentModeScaleAspectFitUIViewContentModeCenter

回答by Declan McKenna

Change the UIImage's Mode from 'scale to fill' to 'Center' within the interface builder. Make sure your image is the exact size you need.

在界面构建器中将 UIImage 的模式从“缩放填充”更改为“居中”。确保您的图像是您需要的确切尺寸。

enter image description here

在此处输入图片说明

回答by Kaushik Movaliya

You have to set CGSize as your image width and hight so image will not stretch and it will arrange at the middle of imageview.

你必须将 CGSize 设置为你的图像宽度和高度,这样图像就不会拉伸,它会排列在图像视图的中间。

- (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size
{
    CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
    CGFloat width = image.size.width * scale;
    CGFloat height = image.size.height * scale;
    CGRect imageRect = CGRectMake((size.width - width)/2.0f,
                                  (size.height - height)/2.0f,
                                  width,
                                  height);

    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    [image drawInRect:imageRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

回答by transistor

Update for Swift 3:

Swift 3 更新:

imageView.contentMode = .scaleAspectFit

回答by Megaetron

How to use original size of image- without any stretching

如何使用图像的原始大小- 没有任何拉伸

  • Just change the UIImage's Mode from scale to fillto Aspect Fitwithin the interface builder.
  • 刚刚从改变的UIImage的模式scale to fill,以Aspect Fit在界面生成器中。

enter image description here

在此处输入图片说明

回答by Nathanial Woolls

Change the contentMode, e.g.:

更改 contentMode,例如:

imageView.contentMode = UIViewContentModeScaleAspectFit;

回答by Baraiya Bhadresh

Use this for your UIImageView

将此用于您的 UIImageView

imageView.contentMode = UIViewContentModeScaleAspectFill;

You won't get any space and with scale preserved. However, some part of the image will be clipped off.

您将不会获得任何空间并保留比例。但是,图像的某些部分将被剪掉。

If you use the following:

如果您使用以下内容:

imageView.contentMode = UIViewContentModeScaleAspectFit;

There will be some empty space, but scale is preserved.

会有一些空白空间,但保留了比例。

回答by Daniel Long

still stretch when image is bigger than imageivew

当图像大于 imageivew 时仍然拉伸

swift

迅速

    let qCodeImage = UIImage(named: "qcode-placeholder.png")!      
    let qCodeImageView = UIImageView(frame: CGRectMake(0, 0, CGRectGetWidth(cardView.frame)-30, CGRectGetWidth(cardView.frame)-30))
    qCodeImageView.image = qCodeImage
    qCodeImageView.clipsToBounds = true
    qCodeImageView.contentMode = UIViewContentMode.ScaleToFill
    qCodeImageView.center = cardView.center