UIImageView:将大小更改为图像大小?

时间:2022-11-11 12:30:19

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

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.

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

How can I do this?

我怎样才能做到这一点?

4 个解决方案

#1


28  

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;

calculate scale ratio on width

计算宽度的比例

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
}

#2


20  

Based on Michael's answer, here's a complete method

根据Michael的回答,这是一个完整的方法

+ (CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
    float widthScale = 0;
    float heightScale = 0;

    widthScale = boxSize.width/originalSize.width;
    heightScale = boxSize.height/originalSize.height;

    float scale = MIN(widthScale, heightScale);

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);

    return newSize;
}

#3


3  

Edit for Steven Stefanik's answer: This method breaks when originalSize is {0, 0}. Maybe consider these changes

编辑Steven Stefanik的回答:当originalSize为{0,0}时,此方法会中断。也许考虑这些变化

-(CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
    if (originalSize.height == 0) {
        originalSize.height = boxSize.height;
    }
    if (originalSize.width == 0) {
        originalSize.width = boxSize.width;
    }

    float widthScale = 0;
    float heightScale = 0;

    widthScale = boxSize.width/originalSize.width;
    heightScale = boxSize.height/originalSize.height;

    float scale = MIN(widthScale, heightScale);

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);

    return newSize;
}

#4


0  

Last edit:

It seems I misunderstood the question.

我似乎误解了这个问题。

To get the actual size you could try:

要获得实际尺寸,您可以尝试:

CGSize imageSize = img.size;
CGSize viewSize = imageView.frame.size;
CGSize actualSize;

if (imageSize.width > imageSize.height) {
    actualSize.width = imageSize.width > viewSize.width ? viewSize.width : imageSize.width;

    actualSize.height = imageSize.height * actualSize.width / imageSize.width;
}
else {
    actualSize.height = imageSize.height > viewSize.height ? viewSize.height : imageSize.height;

    actualSize.width = imageSize.width * actualSize.height / imageSize.height;
}

#1


28  

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;

calculate scale ratio on width

计算宽度的比例

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
}

#2


20  

Based on Michael's answer, here's a complete method

根据Michael的回答,这是一个完整的方法

+ (CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
    float widthScale = 0;
    float heightScale = 0;

    widthScale = boxSize.width/originalSize.width;
    heightScale = boxSize.height/originalSize.height;

    float scale = MIN(widthScale, heightScale);

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);

    return newSize;
}

#3


3  

Edit for Steven Stefanik's answer: This method breaks when originalSize is {0, 0}. Maybe consider these changes

编辑Steven Stefanik的回答:当originalSize为{0,0}时,此方法会中断。也许考虑这些变化

-(CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
    if (originalSize.height == 0) {
        originalSize.height = boxSize.height;
    }
    if (originalSize.width == 0) {
        originalSize.width = boxSize.width;
    }

    float widthScale = 0;
    float heightScale = 0;

    widthScale = boxSize.width/originalSize.width;
    heightScale = boxSize.height/originalSize.height;

    float scale = MIN(widthScale, heightScale);

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);

    return newSize;
}

#4


0  

Last edit:

It seems I misunderstood the question.

我似乎误解了这个问题。

To get the actual size you could try:

要获得实际尺寸,您可以尝试:

CGSize imageSize = img.size;
CGSize viewSize = imageView.frame.size;
CGSize actualSize;

if (imageSize.width > imageSize.height) {
    actualSize.width = imageSize.width > viewSize.width ? viewSize.width : imageSize.width;

    actualSize.height = imageSize.height * actualSize.width / imageSize.width;
}
else {
    actualSize.height = imageSize.height > viewSize.height ? viewSize.height : imageSize.height;

    actualSize.width = imageSize.width * actualSize.height / imageSize.height;
}