简体   繁体   English

图像不适合UIImageView的框架

[英]Image is not fit to the frame of UIImageView

I am creating the UIImageView using interface Builder. 我正在使用界面生成器创建UIImageView Its frame size is (320,67). 其框架尺寸为(320,67)。 I want to display the image on the imageView. 我想在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 问题是从web获取的图像被拉伸到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上显示?

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. 第二个将显示整个图像,可能会使框架的区域打开。

For Swift : 对于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. ScaleAspectFit选项用于通过保持纵横比来缩放内容以适合视图的大小。 Any remaining areas of the view's bounds is transparent. 视图边界的任何剩余区域都是透明的。

截图

Refer Apple docs for details. 有关详细信息,请参阅Apple文档

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)

试试这个代码。

imView.contentMode = UIViewContentModeScaleAspectFit;

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

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