Skip to content

Instantly share code, notes, and snippets.

@pietrorea
Created April 16, 2020 03:13
Show Gist options
  • Save pietrorea/d1e2b7b1ed02b6fd1f4bf72e1fb2f95e to your computer and use it in GitHub Desktop.
Save pietrorea/d1e2b7b1ed02b6fd1f4bf72e1fb2f95e to your computer and use it in GitHub Desktop.
UIImage from UIColor
#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