Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage resize (Scale proportion) [duplicate]

Possible Duplicate:
Resize UIImage with aspect ratio?

The following piece of code is resizing the image perfectly, but the problem is that it messes up the aspect ratio (resulting in a skewed image). Any pointers?

// 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; } 
like image 222
Mustafa Avatar asked Apr 15 '10 13:04

Mustafa


2 Answers

I used this single line of code to create a new UIImage which is scaled. Set the scale and orientation params to achieve what you want. The first line of code just grabs the image.

    // 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)]; 
like image 194
Mark24x7 Avatar answered Oct 16 '22 20:10

Mark24x7


That's ok not a big problem . thing is u got to find the proportional width and height

like if size is 2048.0 x 1360.0 which has to be resized to 320 x 480 resolution then the resulting image size should be 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) 

U can submit in this web page . ARC

Confused ? alright , here is category for UIImage which will do the thing for you.

@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 

-- the following is appropriate call to do this if img is the UIImage instance.

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

like image 43
RamaKrishna Chunduri Avatar answered Oct 16 '22 19:10

RamaKrishna Chunduri