Skip to content
Browse files

Reimplement image tinting

Use the color blend mode to tint the image instead of unilaterally
replacing all non-transparent pixels with the given color.
Remove the -imageTintedWithColor:fraction: method as it does not
make sense with this variety of tinting.
  • Loading branch information...
1 parent b06c8c3 commit 7c7f10092e21e9607843f96b2e2f54380f26df28 @kballard committed Jul 6, 2010
Showing with 6 additions and 18 deletions.
  1. +0 −1 Classes/UIImage+Tint.h
  2. +6 −17 Classes/UIImage+Tint.m
View
1 Classes/UIImage+Tint.h
@@ -10,6 +10,5 @@
@interface UIImage (MGTint)
- (UIImage *)imageTintedWithColor:(UIColor *)color;
-- (UIImage *)imageTintedWithColor:(UIColor *)color fraction:(CGFloat)fraction;
@end
View
23 Classes/UIImage+Tint.m
@@ -13,40 +13,29 @@ @implementation UIImage (MGTint)
- (UIImage *)imageTintedWithColor:(UIColor *)color
{
- // This method is designed for use with template images, i.e. solid-coloured mask-like images.
- return [self imageTintedWithColor:color fraction:0.0]; // default to a fully tinted mask of the image.
-}
-
-
-- (UIImage *)imageTintedWithColor:(UIColor *)color fraction:(CGFloat)fraction
-{
if (color) {
// Construct new image the same size as this one.
UIImage *image;
UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
CGRect rect = CGRectZero;
rect.size = [self size];
- // Composite tint color at its own opacity.
+ // tint the image
+ [self drawInRect:rect];
[color set];
- UIRectFill(rect);
+ UIRectFillUsingBlendMode(rect, kCGBlendModeColor);
- // Mask tint color-swatch to this image's opaque mask.
- // We want behaviour like NSCompositeDestinationIn on Mac OS X.
- [self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0];
+ // restore alpha channel
+ [self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
- // Finally, composite this image over the tinted mask at desired opacity.
- if (fraction > 0.0) {
- // We want behaviour like NSCompositeSourceOver on Mac OS X.
- [self drawInRect:rect blendMode:kCGBlendModeSourceAtop alpha:fraction];
- }
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
return self;
+
}

0 comments on commit 7c7f100

Please sign in to comment.
Something went wrong with that request. Please try again.