yogevbd 6 years ago
parent
commit
972bba2c91
1 changed files with 6 additions and 19 deletions
  1. 6
    19
      lib/ios/UIImage+tint.m

+ 6
- 19
lib/ios/UIImage+tint.m View File

3
 @implementation UIImage (tint)
3
 @implementation UIImage (tint)
4
 
4
 
5
 - (UIImage *)withTintColor:(UIColor *)color {
5
 - (UIImage *)withTintColor:(UIColor *)color {
6
-	UIGraphicsBeginImageContext(self.size);
7
-	CGContextRef context = UIGraphicsGetCurrentContext();
8
-	
9
-	CGContextTranslateCTM(context, 0, self.size.height);
10
-	CGContextScaleCTM(context, 1.0, -1.0);
11
-	
12
-	CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
13
-	
14
-	// draw alpha-mask
15
-	CGContextSetBlendMode(context, kCGBlendModeNormal);
16
-	CGContextDrawImage(context, rect, self.CGImage);
17
-	
18
-	// draw tint color, preserving alpha values of original image
19
-	CGContextSetBlendMode(context, kCGBlendModeSourceIn);
20
-	[color setFill];
21
-	CGContextFillRect(context, rect);
22
-	
23
-	UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
6
+	UIImage *newImage = [self imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
7
+	UIGraphicsBeginImageContextWithOptions(self.size, NO, newImage.scale);
8
+	[color set];
9
+	[newImage drawInRect:CGRectMake(0, 0, self.size.width, newImage.size.height)];
10
+	newImage = UIGraphicsGetImageFromCurrentImageContext();
24
 	UIGraphicsEndImageContext();
11
 	UIGraphicsEndImageContext();
25
 	
12
 	
26
-	return coloredImage;
13
+	return newImage;
27
 }
14
 }
28
 
15
 
29
 @end
16
 @end