Skip to content

Instantly share code, notes, and snippets.

@shnhrrsn
Created March 10, 2012 19:09
Show Gist options
  • Save shnhrrsn/2012554 to your computer and use it in GitHub Desktop.
Save shnhrrsn/2012554 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface UIImage (Resizing)
- (UIImage*)scaleToSize:(CGSize)size;
@end
#import "UIImage+Resizing.h"
@implementation UIImage (Resizing)
- (UIImage*)scaleToSize:(CGSize)size {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
} else {
UIGraphicsBeginImageContext(size);
}
#else
UIGraphicsBeginImageContext(size);
#endif
[self drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment