简体   繁体   English

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

[英]UIImageView: Change size to image size?

I've an UIImageView with content mode Aspect Fit of size 220x155. 我有一个UIImageView ,其内容模式Aspect Fit的大小为220x155。 I'm dynamically inserting different images in different resolutions, but all larger than the size of the UIImageView. 我正在以不同的分辨率动态插入不同的图像,但都大于UIImageView的大小。 As the content mode is set to Aspect Fit , the image is scaled with respect to the ratio to fit the 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? 我怎样才能做到这一点?

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
}

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;
}

Edit for Steven Stefanik's answer: This method breaks when originalSize is {0, 0}. 编辑Steven Stefanik的回答:当originalSize为{0,0}时,此方法会中断。 Maybe consider these changes 也许考虑这些变化

-(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;
}

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;
}

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

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