1

My problem is that, in my app I have UIImageview, button1, button2. button1 is used to access images from saved photolibrary and button2 is used to store image in database,

But image size is (800x800) which is very large, I want to store it at (50x50) size

How to reduce size of image when button2 is clicked?

Cœur
  • 37,241
  • 25
  • 195
  • 267
sirisha
  • 171
  • 3
  • 12

3 Answers3

2
// grab the original image
UIImage *originalImage = [UIImage imageNamed:@"myImage.png"];

UIImage *scaledImage = [UIImage imageWithCGImage:[originalImage CGImage] scale:50/800 orientation:UIImageOrientationUp];
Sabobin
  • 4,256
  • 4
  • 27
  • 33
2

This category will do the job for you -

UIImage+Additions.h -

@interface UIImage (UIImageAdditions)
- (UIImage*)scaleToSize:(CGSize)size;
@end

UIImage+Additions.m -

@implementation UIImage (UIImageAdditions)

- (UIImage*)scaleToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), self.CGImage);

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;
}
@end
Saurabh
  • 22,743
  • 12
  • 84
  • 133
0
+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

Pls see this previous SO question What's the easiest way to resize/optimize an image size with the iPhone SDK? for further reference

Community
  • 1
  • 1
visakh7
  • 26,380
  • 8
  • 55
  • 69