Created
August 5, 2014 20:56
-
-
Save netoleal/3d57bd08a80a259d713c to your computer and use it in GitHub Desktop.
UIImage+Tint
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
#import <UIKit/UIKit.h> | |
@interface UIImage (Tint) | |
- (UIImage *)tintedImageUsingColor:(UIColor *)tintColor; | |
@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
#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