如何在保持其宽高比的同时调整UIImage的大小

时间:2011-07-15 06:36:40

标签: ios objective-c uiimage uikit

我希望在保持其宽高比的情况下调整UIImage的大小。我编写了以下代码,但它没有按预期工作。

代码

-(UIImage * ) scaleImage: (UIImage * ) image toSize: (CGSize) targetSize {

    CGFloat scaleFactor = 1.0;
    if (image.size.width > targetSize.width || image.size.height > targetSize.height)
        if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or
            scaleFactor = targetSize.height / image.size.height; // scale to fit heigth.
    UIGraphicsBeginImageContext(targetSize);
    CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2, (targetSize.height - image.size.height * scaleFactor) / 2,
        image.size.width * scaleFactor, image.size.height * scaleFactor);
    [image drawInRect: rect];
    UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

这里到底出了什么问题?

6 个答案:

答案 0 :(得分:41)

我在一些项目中使用类似的东西:

- (UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
    CGSize scaledSize = newSize;
    float scaleFactor = 1.0;
    if( image.size.width > image.size.height ) {
        scaleFactor = image.size.width / image.size.height;
        scaledSize.width = newSize.width;
        scaledSize.height = newSize.height / scaleFactor;
    }
    else {
        scaleFactor = image.size.height / image.size.width;
        scaledSize.height = newSize.height;
        scaledSize.width = newSize.width / scaleFactor;
    }

    UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
    CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height );
    [image drawInRect:scaledImageRect];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return scaledImage;
}

答案 1 :(得分:23)

如果要在UI中显示,可以使用Interface Builder并指定“Aspect Fit”属性。

您也可以通过将内容模式设置为UIViewContentModeScaleAspectFit来执行此操作:

imageView.contentMode = UIViewContentModeScaleAspectFit;

答案 2 :(得分:10)

此方法采用图像和最大尺寸。如果最大原始图像尺寸小于指定的最大尺寸,则会返回原始图像,因此不会被炸毁。否则,您将获得一个新图像,其最大尺寸为指定的尺寸,另一个尺寸由原始纵横比确定。举例来说,如果你给它一个1024x768的图像,最大尺寸为640,你会得到一个640x480的版本,如果你给它一个768x1024的图像,最大尺寸为640,你会得到一个480x640的版本。

- (UIImage *)resizeImage:(UIImage *)image
        withMaxDimension:(CGFloat)maxDimension
{
    if (fmax(image.size.width, image.size.height) <= maxDimension) {
        return image;
    }

    CGFloat aspect = image.size.width / image.size.height;
    CGSize newSize;

    if (image.size.width > image.size.height) {
        newSize = CGSizeMake(maxDimension, maxDimension / aspect);
    } else {
        newSize = CGSizeMake(maxDimension * aspect, maxDimension);
    }

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);
    CGRect newImageRect = CGRectMake(0.0, 0.0, newSize.width, newSize.height);
    [image drawInRect:newImageRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

答案 3 :(得分:5)

在我的情况下(我希望在UIView中有一个图像)并保持比例与自动布局的描述方式here很棒。本文最重要的部分是(几乎引用):

constraint = [NSLayoutConstraint constraintWithItem:self.imageView
                                          attribute:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:self.imageView 
                                          attribute:NSLayoutAttributeWidth 
                                         multiplier:ration 
                                           constant:0.0f];

[self.imageView addConstraint:constraint];

唯一的办法是根据图像的实际宽度和高度来计算比例。我发现这种方式简单而优雅。

答案 4 :(得分:0)

- (UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    float ratio = newSize.width/image.size.width;
    [image drawInRect:CGRectMake(0, 0, newSize.width, ratio * image.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    NSLog(@"New Image Size : (%f, %f)", newImage.size.width, newImage.size.height);
    UIGraphicsEndImageContext();
    return newImage;
}

答案 5 :(得分:0)

Swift 5 中,您可以在 scaleAspectFit 属性上使用 contentMode

let photo = UIImage(imageLiteralResourceName: "your_img_name")
let photoImageView = UIImageView(image: photo)     
        
photoImageView.contentMode = .scaleAspectFit