123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // _____ _
- // |_ _| |_ _ _ ___ ___ _ __ __ _
- // | | | ' \| '_/ -_) -_) ' \/ _` |_
- // |_| |_||_|_| \___\___|_|_|_\__,_(_)
- //
- // Threema iOS Client
- // Copyright (c) 2014-2020 Threema GmbH
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License, version 3,
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- #import "UIImage+ColoredImage.h"
- @implementation UIImage (ColoredImage)
- static CGFloat scale = -1.0;
- static NSCache *imageCache = nil;
- + (UIImage *) imageNamed:(NSString *)name inColor: (UIColor *) color
- {
- NSString *cacheKey = [NSString stringWithFormat:@"%@/%@", name, color];
-
- // Check cache first
- UIImage *image;
- if (imageCache != nil) {
- image = [imageCache objectForKey:cacheKey];
- if (image != nil) {
- return image;
- }
- }
-
- image = [[UIImage imageNamed:name] imageWithTint: color];
-
- // Put in cache
- if (imageCache == nil) {
- imageCache = [[NSCache alloc] init];
- imageCache.name = @"ColoredImage cache";
- }
- [imageCache setObject:image forKey:cacheKey];
-
- return image;
- }
- - (UIImage *) imageWithTint:(UIColor *)tintColor {
- if (@available(iOS 13.0, *)) {
- return [self imageWithTintColor:tintColor];
- }
-
- return [self drawImageWithTintColor:tintColor];
- }
- // Only use this function before iOS 13 or for non icon images
- - (UIImage *)drawImageWithTintColor:(UIColor *)tintColor {
- if (scale<0.0) {
- UIScreen *screen = [UIScreen mainScreen];
- scale = [screen scale];
- }
-
- CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
-
- UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
- CGContextRef context = UIGraphicsGetCurrentContext();
-
- CGContextTranslateCTM(context, 0, self.size.height);
- CGContextScaleCTM(context, 1.0, -1.0);
-
- CGContextSetBlendMode(context, kCGBlendModeNormal);
- CGContextDrawImage(context, rect, self.CGImage);
-
- CGContextSetBlendMode(context, kCGBlendModeSourceIn);
- [tintColor setFill];
- CGContextFillRect(context, rect);
-
- UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return coloredImage;
- }
- - (UIImage *) invertedImage {
- CIImage *img = [CIImage imageWithCGImage:self.CGImage];
- CIFilter *filter = [CIFilter filterWithName:@"CIColorInvert"];
- [filter setDefaults];
- [filter setValue:img forKey:@"inputImage"];
- CIContext *context = [[CIContext alloc] init];
- CGImageRef ref = [context createCGImage:filter.outputImage fromRect:filter.outputImage.extent];
- return [UIImage imageWithCGImage:ref];
- }
- @end
|