Created
October 6, 2014 15:05
-
-
Save benvium/582332941ca47039dd57 to your computer and use it in GitHub Desktop.
Re-color a UIImage source: http://stackoverflow.com/questions/12396236/ios-change-the-colors-of-a-uiimage
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 <UIKit/UIKit.h> | |
@interface UIImage (AddtionalFunctionalities) | |
//TintColor... | |
- (UIImage *)imageWithTint:(UIColor *)tintColor; | |
//scale and resize... | |
-(UIImage*)scaleToSize:(CGSize)size; | |
@end |
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+AddtionalFunctionalities.h" | |
@implementation UIImage (AddtionalFunctionalities) | |
- (UIImage *)imageWithTint:(UIColor *)tintColor | |
{ | |
// Begin drawing | |
CGRect aRect = CGRectMake(0.f, 0.f, self.size.width, self.size.height); | |
CGImageRef alphaMask; | |
// | |
// Compute mask flipping image | |
// | |
{ | |
UIGraphicsBeginImageContext(aRect.size); | |
CGContextRef c = UIGraphicsGetCurrentContext(); | |
// draw image | |
CGContextTranslateCTM(c, 0, aRect.size.height); | |
CGContextScaleCTM(c, 1.0, -1.0); | |
[self drawInRect: aRect]; | |
alphaMask = CGBitmapContextCreateImage(c); | |
UIGraphicsEndImageContext(); | |
} | |
// | |
UIGraphicsBeginImageContext(aRect.size); | |
// Get the graphic context | |
CGContextRef c = UIGraphicsGetCurrentContext(); | |
// Draw the image | |
[self drawInRect:aRect]; | |
// Mask | |
CGContextClipToMask(c, aRect, alphaMask); | |
// Set the fill color space | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextSetFillColorSpace(c, colorSpace); | |
// Set the fill color | |
CGContextSetFillColorWithColor(c, tintColor.CGColor); | |
UIRectFillUsingBlendMode(aRect, kCGBlendModeNormal); | |
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
// Release memory | |
CGColorSpaceRelease(colorSpace); | |
CGImageRelease(alphaMask); | |
return img; | |
} | |
-(UIImage*)scaleToSize:(CGSize)size | |
{ | |
// Create a bitmap graphics context | |
// This will also set it as the current context | |
UIGraphicsBeginImageContext(size); | |
// Draw the scaled image in the current context | |
[self drawInRect:CGRectMake(0, 0, size.width, size.height)]; | |
// Create a new image from current context | |
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); | |
// Pop the current context from the stack | |
UIGraphicsEndImageContext(); | |
// Return our new scaled image | |
return scaledImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment