Skip to content

Instantly share code, notes, and snippets.

@takkyun
Created May 29, 2014 07:27
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 takkyun/9657b5f5153c664bb71a to your computer and use it in GitHub Desktop.
Save takkyun/9657b5f5153c664bb71a to your computer and use it in GitHub Desktop.
@interface SBLImageHelper
/**
* Generates an image which is tinted with a given color.
* @param image - an image to be tinted
* @param color - tint color
* @return an instance of UIImage
*/
+ (UIImage *)tintedImageFromImage:(UIImage *)image
withColor:(UIColor *)color;
@end
@implementation SBLImageHelper
+ (UIImage *)tintedImageFromImage:(UIImage *)image
withColor:(UIColor *)color
{
UIImage *output = nil;
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
if (UIGraphicsBeginImageContextWithOptions != NULL)
{
UIGraphicsBeginImageContextWithOptions(image.size,
NO,
image.scale);
}
else
{
UIGraphicsBeginImageContext(image.size);
}
CGContextRef context = UIGraphicsGetCurrentContext();
// -- flipping geometry
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// -- setting up blend mode
CGContextSetBlendMode(context, kCGBlendModeNormal);
// -- first drawing the image
CGContextDrawImage(context, rect, image.CGImage);
// -- then setting up mask and fill the color
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
output = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return output;
}
@end
UIColor *tintColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
UIImage *originalImage = [UIImage imageNamed:@"sample"];
UIImage *tintedImage = [SBLImageHelper tintedImageFromImage:originalImage
withColor:tintColor];
@interface UIImage (SBLImageHelper)
/**
* Generates an image which is tinted with a given color.
* @param color - tint color
* @return an instance of UIImage
*/
- (UIImage *)imageWithTintColor:(UIColor *)color;
@end
@implementation UIImage (SBLImageHelper)
- (UIImage *)imageWithTintColor:(UIColor *)color
{
UIImage *output = nil;
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
if (UIGraphicsBeginImageContextWithOptions != NULL)
{
UIGraphicsBeginImageContextWithOptions(self.size,
NO,
self.scale);
}
else
{
UIGraphicsBeginImageContext(self.size);
}
CGContextRef context = UIGraphicsGetCurrentContext();
// -- flipping geometry
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// -- setting up blend mode
CGContextSetBlendMode(context, kCGBlendModeNormal);
// -- first drawing the image
CGContextDrawImage(context, rect, self.CGImage);
// -- then setting up mask and fill the color
CGContextClipToMask(context, rect, self.CGImage);
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
output = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return output;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment