Created
January 8, 2012 22:18
-
-
Save basecode/1579913 to your computer and use it in GitHub Desktop.
UIImage Category: Image with UIColor overlay (kCGBlendModeColor, ARC enabled)
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
// UIImage+ColorOverlayCategory.h | |
// Created by Tobias Reiss, @basecode on 1/8/12. | |
#import <UIKit/UIKit.h> | |
@interface UIImage (ColorOverlayCategory) | |
- (UIImage*)imageWithColorOverlay:(UIColor*)colorOverlay; | |
@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
// UIImage+ColorOverlayCategory.m | |
// Created by Tobias Reiss, @basecode on 1/8/12. | |
// | |
#import <UIKit/UIKit.h> | |
@implementation UIImage (ColorOverlayCategory) | |
- (UIImage*)imageWithColorOverlay:(UIColor*)colorOverlay | |
{ | |
// create drawing context | |
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); | |
// draw current image | |
[self drawAtPoint:CGPointZero]; | |
// determine bounding box of current image | |
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); | |
// get drawing context | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// flip orientation | |
CGContextTranslateCTM(context, 0.0, self.size.height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
// set overlay | |
CGContextSetBlendMode(context, kCGBlendModeColor); | |
CGContextClipToMask(context, rect, self.CGImage); | |
CGContextSetFillColorWithColor(context, colorOverlay.CGColor); | |
CGContextFillRect(context, rect); | |
// save drawing-buffer | |
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext(); | |
// end drawing context | |
UIGraphicsEndImageContext(); | |
return returnImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment