Skip to content

Instantly share code, notes, and snippets.

@stephsharp
Last active August 29, 2017 19:32
Show Gist options
  • Save stephsharp/51a5d44cca5541c19878 to your computer and use it in GitHub Desktop.
Save stephsharp/51a5d44cca5541c19878 to your computer and use it in GitHub Desktop.
UIImage+Tint
//
// UIImage+Tint.h
// Created by Stephanie Sharp on 5/03/2014.
//
#import <UIKit/UIKit.h>
@interface UIImage (Tint)
- (UIImage *)translucentImageWithAlpha:(CGFloat)alpha;
- (UIImage *)tintedGradientImageWithColor:(UIColor *)tintColor;
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor;
@end
//
// UIImage+Tint.m
// Created by Stephanie Sharp on 5/03/2014.
//
// See: http://stackoverflow.com/a/7543459/1367622
// http://robots.thoughtbot.com/designing-for-ios-blending-modes
//
#import "UIImage+Tint.h"
@implementation UIImage (Tint)
#pragma mark - Public methods
// http://stackoverflow.com/a/7543459/1367622
- (UIImage *)translucentImageWithAlpha:(CGFloat)alpha
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
[self drawInRect:bounds blendMode:kCGBlendModeScreen alpha:alpha];
UIImage * translucentImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return translucentImage;
}
- (UIImage *)tintedGradientImageWithColor:(UIColor *)tintColor
{
return [self tintedImageWithColor:tintColor blendingMode:kCGBlendModeOverlay];
}
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor
{
return [self tintedImageWithColor:tintColor blendingMode:kCGBlendModeDestinationIn];
}
#pragma mark - Private methods
// http://robots.thoughtbot.com/designing-for-ios-blending-modes
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor blendingMode:(CGBlendMode)blendMode
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
[self drawInRect:bounds blendMode:blendMode alpha:1.0f];
// if blend mode was overlay, restore image alpha
if (blendMode != kCGBlendModeDestinationIn)
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0];
UIImage * tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
@end
@stephsharp
Copy link
Author

See also https://stackoverflow.com/a/7377827/1367622

- (UIImage *)tintedImageWithColor:(UIColor *)tintColor
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    
    // draw tint color
    [tintColor setFill];
    UIRectFill(bounds);
    
    // replace luminosity of background (ignoring alpha)
    [self drawInRect:bounds blendMode:kCGBlendModeLuminosity alpha:1.0];
    
    // mask by alpha values of original image
    [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0];
    
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return tintedImage;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment