【问题标题】:UIImage resize (Scale proportion) [duplicate]UIImage调整大小(缩放比例)[重复]
【发布时间】:2011-02-08 09:37:05
【问题描述】:

可能重复:
Resize UIImage with aspect ratio?

以下代码完美地调整了图像的大小,但问题是它弄乱了纵横比(导致图像倾斜)。有什么指点吗?

// Change image resolution (auto-resize to fit)
+ (UIImage *)scaleImage:(UIImage*)image toResolution:(int)resolution {
    CGImageRef imgRef = [image CGImage];
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
    CGRect bounds = CGRectMake(0, 0, width, height);

    //if already at the minimum resolution, return the orginal image, otherwise scale
    if (width <= resolution && height <= resolution) {
        return image;

    } else {
        CGFloat ratio = width/height;

        if (ratio > 1) {
            bounds.size.width = resolution;
            bounds.size.height = bounds.size.width / ratio;
        } else {
            bounds.size.height = resolution;
            bounds.size.width = bounds.size.height * ratio;
        }
    }

    UIGraphicsBeginImageContext(bounds.size);
    [image drawInRect:CGRectMake(0.0, 0.0, bounds.size.width, bounds.size.height)];
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;
}

【问题讨论】:

  • 查看这个问题(以及我的回答;):stackoverflow.com/questions/1703100/…
  • 其他 SO 问题的链接并不是真正的好答案。如果这是重复的,则应照此关闭。
  • 这帮助我解决了需要缩放 UIImage 的问题,谢谢 :)

标签: iphone resize uiimage


【解决方案1】:

我使用这行代码创建了一个新的 UIImage,它是缩放的。设置比例和方向参数以实现您想要的。第一行代码只是抓取图像。

    // grab the original image
    UIImage *originalImage = [UIImage imageNamed:@"myImage.png"];
    // scaling set to 2.0 makes the image 1/2 the size. 
    UIImage *scaledImage = 
                [UIImage imageWithCGImage:[originalImage CGImage] 
                              scale:(originalImage.scale * 2.0)
                                 orientation:(originalImage.imageOrientation)];

【讨论】:

  • 这是一个逻辑调整大小。如果您希望为性能目的调整图像大小,请查看 vocaro.com/trevor/blog/2009/10/12/…,如果您在 iOS 5 中遇到错误行为,请查看 eran.sandler.co.il/2011/11/07/…
  • 显然是 Abdul,否则 36 人不会投赞成票。你做错了什么。
  • 缩放到 2.0 使图像大小变为原来的 1/2...哈哈!
  • 快速:let img: UIImage = UIImage(named: "lena")! var scaledImage = UIImage(CGImage: img.CGImage!, scale: img.scale * 2, orientation: img.imageOrientation)
  • @zonabi,没那么好笑。除以 2.0 而不是相乘怎么样...UIImage *scaledImage = [UIImage imageWithCGImage:[image CGImage] scale:image.scale/2.0 orientation:(image.imageOrientation)]; 这样您就可以更合乎逻辑地表达您的比例需求(又名“我想将比例与 n 因子相乘”)
【解决方案2】:

没关系,问题不大。问题是你必须找到成比例的宽度和高度

如果尺寸为 2048.0 x 1360.0,必须将其调整为 320 x 480 分辨率,则生成的图像尺寸应为 722.0 x 480.0

here is the formulae to do that . if w,h is original and x,y are resulting image.
w/h=x/y 
=> 
x=(w/h)*y;  

submitting w=2048,h=1360,y=480 => x=722.0 ( here width>height. if height>width then consider x to be 320 and calculate y)

你可以在这个网页上提交。 ARC

困惑?好的,这是 UIImage 的类别,它会为你做这件事。

@interface UIImage (UIImageFunctions)
    - (UIImage *) scaleToSize: (CGSize)size;
    - (UIImage *) scaleProportionalToSize: (CGSize)size;
@end
@implementation UIImage (UIImageFunctions)

- (UIImage *) scaleToSize: (CGSize)size
{
    // Scalling selected image to targeted size
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    CGContextClearRect(context, CGRectMake(0, 0, size.width, size.height));

    if(self.imageOrientation == UIImageOrientationRight)
    {
        CGContextRotateCTM(context, -M_PI_2);
        CGContextTranslateCTM(context, -size.height, 0.0f);
        CGContextDrawImage(context, CGRectMake(0, 0, size.height, size.width), self.CGImage);
    }
    else
        CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), self.CGImage);

    CGImageRef scaledImage=CGBitmapContextCreateImage(context);

    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);

    UIImage *image = [UIImage imageWithCGImage: scaledImage];

    CGImageRelease(scaledImage);

    return image;
}

- (UIImage *) scaleProportionalToSize: (CGSize)size1
{
    if(self.size.width>self.size.height)
    {
        NSLog(@"LandScape");
        size1=CGSizeMake((self.size.width/self.size.height)*size1.height,size1.height);
    }
    else
    {
        NSLog(@"Potrait");
        size1=CGSizeMake(size1.width,(self.size.height/self.size.width)*size1.width);
    }

    return [self scaleToSize:size1];
}

@end

-- 如果 img 是 UIImage 实例,则以下是适当的调用。

img=[img scaleProportionalToSize:CGSizeMake(320, 480)];

【讨论】:

  • 你的意思是anImageView.contentMode=UIViewContentModeScaleAspectFit;?
  • 是的,类似于内容模式,但不使用任何 UI 元素,因此缩放后的图像可以进一步存储到文件或数据库中......
【解决方案3】:

这修正了将数学缩放到宽度和高度的最大尺寸,而不仅仅是一个取决于原始宽度和高度的尺寸。

- (UIImage *) scaleProportionalToSize: (CGSize)size
{
    float widthRatio = size.width/self.size.width;
    float heightRatio = size.height/self.size.height;

    if(widthRatio > heightRatio)
    {
        size=CGSizeMake(self.size.width*heightRatio,self.size.height*heightRatio);
    } else {
        size=CGSizeMake(self.size.width*widthRatio,self.size.height*widthRatio);
    }

    return [self scaleToSize:size];
}

【讨论】:

    【解决方案4】:

    这个改变对我有用:

    // The size returned by CGImageGetWidth(imgRef) & CGImageGetHeight(imgRef) is incorrect as it doesn't respect the image orientation!
    // CGImageRef imgRef = [image CGImage];
    // CGFloat width = CGImageGetWidth(imgRef);
    // CGFloat height = CGImageGetHeight(imgRef);
    //
    // This returns the actual width and height of the photo (and hence solves the problem
    CGFloat width = image.size.width; 
    CGFloat height = image.size.height;
    CGRect bounds = CGRectMake(0, 0, width, height);
    

    【讨论】:

      【解决方案5】:

      尝试将bounds 的大小设为整数。

      #include <math.h>
      ....
      
          if (ratio > 1) {
              bounds.size.width = resolution;
              bounds.size.height = round(bounds.size.width / ratio);
          } else {
              bounds.size.height = resolution;
              bounds.size.width = round(bounds.size.height * ratio);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        • 2023-03-22
        • 2012-08-29
        • 2023-02-03
        • 2015-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多