Created
May 29, 2014 07:27
-
-
Save takkyun/9657b5f5153c664bb71a to your computer and use it in GitHub Desktop.
Tinted image (UIImage) ref: http://qiita.com/takkyun@github/items/86a70656981d7dca1088
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
@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 |
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
UIColor *tintColor = [[UIColor redColor] colorWithAlphaComponent:0.5]; | |
UIImage *originalImage = [UIImage imageNamed:@"sample"]; | |
UIImage *tintedImage = [SBLImageHelper tintedImageFromImage:originalImage | |
withColor:tintColor]; |
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
@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