Skip to content

Instantly share code, notes, and snippets.

@sanmai sanmai/UIImage+Scale.h
Last active Dec 23, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.