Last active
January 20, 2021 23:23
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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