Created
December 18, 2015 12:38
-
-
Save bigeyex/2d23202b6fdae3dfe004 to your computer and use it in GitHub Desktop.
UIImage+Tint: tint an UIImage
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
#import <Foundation/Foundation.h> | |
@interface UIImage (Tint) | |
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor; | |
@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
#import "UIImage+Tint.h" | |
@implementation UIImage (Tint) | |
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor { | |
UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextTranslateCTM(context, 0, self.size.height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); | |
// draw alpha-mask | |
CGContextSetBlendMode(context, kCGBlendModeNormal); | |
CGContextDrawImage(context, rect, self.CGImage); | |
// draw tint color, preserving alpha values of original image | |
CGContextSetBlendMode(context, kCGBlendModeSourceIn); | |
[tintColor setFill]; | |
CGContextFillRect(context, rect); | |
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return coloredImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment