Changing The Color Of Your Image Programmatically In iOS Objective-C

Hi,  how's your day? I'm going to share for today a method for changing the color/colour of a certain image. I can't give you an example input/output for now, my laptop is a bit slow for me to open up Xcode and run the method. I got this code by the way from stackoverflow. Here, check it out:

- (UIImage *)colorImage:(UIImage *)origImage withColor:(UIColor *)color
    UIGraphicsBeginImageContextWithOptions(origImage.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, (CGRect){ {0,0}, origImage.size} );

    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, origImage.size.height);
    CGContextConcatCTM(context, flipVertical);
    CGContextDrawImage(context, (CGRect){ pt, origImage.size }, [origImage CGImage]);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    return image;

Post a Comment