代码之家  ›  专栏  ›  技术社区  ›  Mustafa

uiimage resize(缩放比例)[重复]

  •  47
  • Mustafa  · 技术社区  · 15 年前

    可能重复:
    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;
    }
    
    5 回复  |  直到 12 年前
        1
  •  112
  •   Ned    13 年前

    我用这一行代码创建了一个新的可缩放的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)];
    
        2
  •  15
  •   lewisanderson    14 年前

    没关系,没什么大问题。你得找出比例宽度和高度

    例如,如果大小是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标度比例大小:cgsizemake(320,480)];

        3
  •  5
  •   Monte Goulding    13 年前

    这将数学修正为在宽度和高度上都缩放到最大大小,而不仅仅是一个取决于原始的宽度和高度。

    - (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
  •  5
  •   Mustafa    12 年前

    这一变化对我起了作用:

    // 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
  •  0
  •   kennytm    15 年前

    尝试使 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);
        }