It's not a problem. the thing you got to find proportional width and height
as if the size were 2048.0 x 1360.0, which should be resized to 320 x 480, 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 send on this web page. ARC
Embarrassed? ok, here is a category for UIImage that will help 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 next call is the corresponding call if img is an instance of UIImage.
img = [img scaleProportionalToSize: CGSizeMake (320, 480)];