Skip to content

Instantly share code, notes, and snippets.

@bigeyex
Created December 18, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigeyex/2d23202b6fdae3dfe004 to your computer and use it in GitHub Desktop.
Save bigeyex/2d23202b6fdae3dfe004 to your computer and use it in GitHub Desktop.
UIImage+Tint: tint an UIImage
#import <Foundation/Foundation.h>
@interface UIImage (Tint)
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor;
@end
#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