Skip to content

Commit

Permalink
Reimplement image tinting
Browse files Browse the repository at this point in the history
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
lilyball committed Jul 6, 2010
1 parent b06c8c3 commit 7c7f100
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
1 change: 0 additions & 1 deletion Classes/UIImage+Tint.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
@interface UIImage (MGTint)

- (UIImage *)imageTintedWithColor:(UIColor *)color;
- (UIImage *)imageTintedWithColor:(UIColor *)color fraction:(CGFloat)fraction;

@end
23 changes: 6 additions & 17 deletions Classes/UIImage+Tint.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ @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.
Expand All @@ -27,26 +20,22 @@ - (UIImage *)imageTintedWithColor:(UIColor *)color fraction:(CGFloat)fraction
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;

}


Expand Down

0 comments on commit 7c7f100

Please sign in to comment.