From f0f34293b9df9e23eaa5b692a9db68840cc64038 Mon Sep 17 00:00:00 2001 From: Colin Brash Date: Fri, 9 Apr 2021 05:09:48 -0400 Subject: [PATCH] fix: sharpen blurry icons across the app (#1488) Fix the `image(with color)` extension method so it does not scale the image incorrectly. The new implementation uses an imageView to render the new image. --- Source/UIImage+OEXHelpers.swift | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Source/UIImage+OEXHelpers.swift b/Source/UIImage+OEXHelpers.swift index 43cd889e43..f863488225 100644 --- a/Source/UIImage+OEXHelpers.swift +++ b/Source/UIImage+OEXHelpers.swift @@ -59,18 +59,20 @@ extension UIImage { } func image(with color: UIColor) -> UIImage { - let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) - UIGraphicsBeginImageContext(rect.size) - let context = UIGraphicsGetCurrentContext() - if let cgImage = cgImage { - context?.clip(to: rect, mask: cgImage) + let image = withRenderingMode(.alwaysTemplate) + let imageView = UIImageView(image: image) + imageView.tintColor = color + + var tintedImage: UIImage? + UIGraphicsBeginImageContextWithOptions(image.size, false, 0.0) + if let context = UIGraphicsGetCurrentContext() { + imageView.layer.render(in: context) + tintedImage = UIGraphicsGetImageFromCurrentImageContext() } - context?.setFillColor(color.cgColor) - context?.fill(rect) + UIGraphicsEndImageContext() - if let cgImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage { - UIGraphicsEndImageContext() - return UIImage(cgImage: cgImage, scale: 1, orientation: .downMirrored) + if let tintedImage = tintedImage { + return tintedImage } else { return self }