Skip to content

Instantly share code, notes, and snippets.

@e23z
Created July 26, 2017 14:45
Show Gist options
  • Select an option

  • Save e23z/b99d8592c465e365b48ae7746f814ce9 to your computer and use it in GitHub Desktop.

Select an option

Save e23z/b99d8592c465e365b48ae7746f814ce9 to your computer and use it in GitHub Desktop.
[Tint Image in CALayer] How to tint an image that is in a CALayer object. #swift #mobile #ios #ui
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor blendingMode:(CGBlendMode)blendMode highQuality:(BOOL) yerOrNo;
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
if (yerOrNo) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(context, true);
CGContextSetAllowsAntialiasing(context, true);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
}
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
[self drawInRect:bounds blendMode:blendMode alpha:1.0f];
if (blendMode != kCGBlendModeDestinationIn)
[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