ios 图像不适合 UIImageView 的框架

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

Image is not fit to the frame of UIImageView

iphoneiosobjective-cipaduiimageview

提问by Venk

I am creating the UIImageViewusing interface Builder. Its frame size is (320,67). I want to display the image on the imageView. I am getting the image from the web. The problem is that the image get from web is stretched to display on the imageview... Here my code

我正在创建UIImageView使用界面生成器。它的帧大小是 (320,67)。我想在 imageView 上显示图像。我正在从网络获取图像。问题是从网络获取的图像被拉伸以显示在图像视图上......这是我的代码

NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.isco.com/webproductimages/appBnr/bnr1.jpg"]];
imageView.image = [UIImage imageWithData:imageData];

Can anyone tel me that how to display the image fit to display on imageView????

谁能告诉我如何显示适合在 imageView 上显示的图像????

回答by mvds

Either use

要么使用

imageView.contentMode = UIViewContentModeScaleAspectFill;

or

或者

imageView.contentMode = UIViewContentModeScaleAspectFit;

The first one will fill the frame, possibly cutting off parts of your image. The second one will show the entire image, possibly leaving areas of the frame open.

第一个将填充框架,可能会切断图像的一部分。第二个将显示整个图像,可能会留下框架区域打开。

回答by Jayprakash Dubey

For Swift :

对于斯威夫特:

imageView.contentMode = UIViewContentMode.ScaleAspectFit

For Objective-C :

对于 Objective-C :

 imageView.contentMode = UIViewContentModeScaleAspectFit

ScaleAspectFit option is use to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining areas of the view's bounds is transparent.

ScaleAspectFit 选项用于通过保持纵横比来缩放内容以适应视图的大小。视图边界的任何剩余区域都是透明的。

Screenshot

截屏

Refer Apple docsfor details.

有关详细信息,请参阅Apple 文档

回答by SaundersB

I ended up resizing the image after the image was scaled according to it's aspect ratio.

根据其纵横比缩放图像后,我最终调整了图像大小。

let widthRatio = ImageView.bounds.size.width / (captureImageView.image?.size.width)!
let heightRatio = ImageView.bounds.size.height / (captureImageView.image?.size.height)!
let scale = min(widthRatio, heightRatio)
let imageWidth = scale * (ImageView.image?.size.width)!
let imageHeight = scale * (ImageView.image?.size.height)!
ImageView.frame = CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)

回答by Nag Raj

Try this code.

试试这个代码。

imView.contentMode = UIViewContentModeScaleAspectFit;