Last active
December 28, 2015 09:59
-
-
Save sag333ar/7482644 to your computer and use it in GitHub Desktop.
Apply color to black-masked-image
This file contains hidden or 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
// pass color & supply masked (masked with black-color) image | |
+ (UIImage *)applyColor:(UIColor *)color toImage:(UIImage*)toImage{ | |
// create context | |
UIGraphicsBeginImageContextWithOptions(toImage.size, NO, toImage.scale); | |
// get context reference | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// Change the origin of the user coordinate system in a context. | |
CGContextTranslateCTM(context, 0, toImage.size.height); | |
// Change the scale of the user coordinate system in a context. | |
CGContextScaleCTM(context, 1.0, -1.0); | |
// Set how blend-normal for a graphics context. | |
CGContextSetBlendMode(context, kCGBlendModeNormal); | |
// get region of rect | |
CGRect rect = CGRectMake(0, 0, toImage.size.width, toImage.size.height); | |
// Create gradient - (but we have single color here) | |
NSArray *colors = [NSArray arrayWithObjects:(id)color.CGColor, (id)color.CGColor, nil]; | |
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); | |
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL); | |
// Apply gradient to mask | |
CGContextClipToMask(context, rect, toImage.CGImage); | |
CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, toImage.size.height), 0); | |
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
CGGradientRelease(gradient); | |
CGColorSpaceRelease(space); | |
return coloredImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment