0

I have tried image resize plugin to compress image in my app. But it reduces the clarity and it doesn't look good. Can anyone please tell me any other solution to compress image dynamically in my iPhone application.

#import <Foundation/Foundation.h>

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


#import "UIImage+Resize.h"


// Put this in UIImageResizing.m
@implementation UIImage (Resize)

- (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
CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
gunas
  • 1,889
  • 1
  • 16
  • 29
  • If you want the image compressed, convert it to a [file image](http://stackoverflow.com/q/11592313/581994) as a PNG or JPEG. You don't have to actually save the file image, you can keep it as NSData to restore quickly to UIImage. – Hot Licks Jan 23 '14 at 13:09
  • And [this algorithm](http://stackoverflow.com/a/6064453/581994) will half-size an image with minimal loss of detail. – Hot Licks Jan 23 '14 at 13:11
  • @HotLicks Interesting. But is it actually better than using a system method? Any rationale for this approach? – CouchDeveloper Jan 23 '14 at 13:14
  • I tried several "system methods" and all produced an inferior image. – Hot Licks Jan 23 '14 at 16:57

0 Answers0