7

I want to reduce image file size that take from UIImagePickerController . I use this method

NSData *imageData = UIImageJPEGRepresentation(image, 0.1);

but it reduce 2.2 MB image file size to 300 KB I want my image file size become less than 100 KB.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
Poooyak
  • 135
  • 1
  • 1
  • 12
  • Check out this question: http://stackoverflow.com/questions/2313428/reduce-image-bytes-size-with-cocoa [1]: http://stackoverflow.com/questions/2313428/reduce-image-bytes-size-with-cocoa – Shahin Dec 29 '12 at 19:24
  • After all I found this solution is the best one http://stackoverflow.com/questions/612131/whats-the-easiest-way-to-resize-optimize-an-image-size-with-the-iphone-sdk – Poooyak Dec 30 '12 at 11:30

3 Answers3

8

Apple's docs state:

The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).

And since the compression quality is a CGFloat, it supports decimal places beyond the tenths place. That being said, try:

NSData *imageData = UIImageJPEGRepresentation(image, 0.032);
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
2

Easiest way to reduce image size in kilos is to reduce the size in pixels! Scale it smaller:

CGFloat scaleSize = 0.2f;
UIImage *smallImage = [UIImage imageWithCGImage:image.CGImage
                      scale:scaleSize
                      orientation:image.imageOrientation];
JOM
  • 8,139
  • 6
  • 78
  • 111
  • 1
    Exactlly! A large image can't be downsized to 100K in jpeg format, even at highest compression level, so idea is to resize it, then compress it. – Lefteris Dec 30 '12 at 00:42
  • @Lefteris I use this method and instead of 0.2 for scalesize I set it 5 to reduce image size 5 times and then use the previous method but the file size didn't change. – Poooyak Dec 30 '12 at 11:14
  • Scale 1 (one) means same size as original image. You should use a value smaller than one, not bigger. – JOM Dec 30 '12 at 20:49
  • this method returns exactly the same image but with a different density maintaining the same file size – Christian Mar 16 '16 at 09:43
1

First resize the image with below method:

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

call this by:

UIImage *newImage=yourImage;
CGSize size=CGSizeMake(newImage.size.width/8,newImage.size.height/8);
newImage=[self resizeImage:newImage newSize:size];

And finally compressed your image as required:

NSData *imageData = UIImageJPEGRepresentation(newImage, 0.5);
NSLog(@"Size of image = %lu KB",(imageData.length/1024));
ajay_nasa
  • 2,278
  • 2
  • 28
  • 45