Skip to content

Instantly share code, notes, and snippets.

@sanmai
Last active January 20, 2021 23:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanmai/6593048 to your computer and use it in GitHub Desktop.
Save sanmai/6593048 to your computer and use it in GitHub Desktop.
If you just want an UIImage smaller and don't care about exact sizing, this category is for you. It extends the UIImage class to support scaling. Methods should be pretty self-explanatory.
// Created by Alexey Kopytko in 2013.
// Public domain with no warranty whatsoever.
// Extends the UIImage class to support scaling
@interface UIImage (Scale)
- (UIImage *)imageScaledToQuarter;
- (UIImage *)imageScaledToHalf;
- (UIImage *)imageScaledToScale:(CGFloat)scale;
- (UIImage *)imageScaledToScale:(CGFloat)scale
withInterpolationQuality:(CGInterpolationQuality)quality;
@end
// Created by Alexey Kopytko in 2013
// Public domain with no warranty whatsoever.
#import "UIImage+Scale.h"
@implementation UIImage (Scale)
- (UIImage *)imageScaledToQuarter
{
return [self imageScaledToScale:0.25f withInterpolationQuality:kCGInterpolationHigh];
}
- (UIImage *)imageScaledToHalf
{
return [self imageScaledToScale:0.5f withInterpolationQuality:kCGInterpolationHigh];
}
- (UIImage *)imageScaledToScale:(CGFloat)scale
{
return [self imageScaledToScale:scale withInterpolationQuality:kCGInterpolationHigh];
}
- (UIImage *)imageScaledToScale:(CGFloat)scale withInterpolationQuality:(CGInterpolationQuality)quality
{
UIGraphicsBeginImageContextWithOptions(self.size, YES, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, quality);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment