Created
April 16, 2020 03:13
-
-
Save pietrorea/d1e2b7b1ed02b6fd1f4bf72e1fb2f95e to your computer and use it in GitHub Desktop.
UIImage from UIColor
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+Extension.h" | |
@implementation UIImage (Extension) | |
- (UIImage *)imageWithColor:(UIColor *)color | |
{ | |
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextTranslateCTM(context, 0, self.size.height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
CGContextSetBlendMode(context, kCGBlendModeNormal); | |
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); | |
CGContextClipToMask(context, rect, self.CGImage); | |
[color setFill]; | |
CGContextFillRect(context, rect); | |
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return newImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment