Skip to content

Instantly share code, notes, and snippets.

@basecode
Created January 8, 2012 22:18
Show Gist options
  • Save basecode/1579913 to your computer and use it in GitHub Desktop.
Save basecode/1579913 to your computer and use it in GitHub Desktop.
UIImage Category: Image with UIColor overlay (kCGBlendModeColor, ARC enabled)
// UIImage+ColorOverlayCategory.h
// Created by Tobias Reiss, @basecode on 1/8/12.
#import <UIKit/UIKit.h>
@interface UIImage (ColorOverlayCategory)
- (UIImage*)imageWithColorOverlay:(UIColor*)colorOverlay;
@end
// 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