Skip to content

Instantly share code, notes, and snippets.

@netoleal
Created August 5, 2014 20:56
Show Gist options
  • Save netoleal/3d57bd08a80a259d713c to your computer and use it in GitHub Desktop.
Save netoleal/3d57bd08a80a259d713c to your computer and use it in GitHub Desktop.
UIImage+Tint
#import <UIKit/UIKit.h>
@interface UIImage (Tint)
- (UIImage *)tintedImageUsingColor:(UIColor *)tintColor;
@end
#import "UIImage+Tint.h"
@implementation UIImage (Tint)
- (UIImage *)tintedImageUsingColor:(UIColor *)color
{
if (color) {
// Construct new image the same size as this one.
UIImage *image;
UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
CGRect rect = CGRectZero;
rect.size = [self size];
// tint the image
[self drawInRect:rect];
[color set];
UIRectFillUsingBlendMode(rect, kCGBlendModeNormal);
// restore alpha channel
[self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment