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.

Revisions

  1. pietrorea created this gist Apr 16, 2020.
    21 changes: 21 additions & 0 deletions UIImage+Extension.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #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