UIImageView:将大小更改为图像大小? [英] UIImageView: Change size to image size?

查看:133
本文介绍了UIImageView:将大小更改为图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UIImageView 内容模式 Aspect Fit ,大小为220x155。我正在以不同的分辨率动态插入不同的图像,但都比UIImageView的大小大。当内容模式设置为 Aspect Fit 时,图像将根据比率进行缩放以适合UIImageView。

I've an UIImageView with content mode Aspect Fit of size 220x155. I'm dynamically inserting different images in different resolutions, but all larger than the size of the UIImageView. As the content mode is set to Aspect Fit, the image is scaled with respect to the ratio to fit the UIImageView.

我的问题是,如果例如UIImageView中的图像缩放到220x100,我希望UIImageView也从155到100的高度缩小,以避免我的元素之间的空间。

My problem is, that if for instance the image inside the UIImageView is scaled to 220x100, I would like the UIImageView to shrink from a height of 155 to 100 too to avoid space between my elements.

我该怎么做?

推荐答案

如果我找对你,那就是这样的:通过以下方式获取图像大小:

If I got you right, it would be something like this: get image size by:

UIImage * img = [UIImage imageNamed:@"someImage.png"];
CGSize imgSize = img.size;

计算宽度比例

float ratio=yourImageView.frame.size.width/imgSize.width;

检查缩放高度(使用相同比率保持方位)

check scaled height (using same ratio to keep aspect)

float scaledHeight=imgSize.height*ratio;
if(scaledHeight < yourImageView.frame.size.height)
{
   //update height of your imageView frame with scaledHeight
}

这篇关于UIImageView:将大小更改为图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆