From 7b3ad58315601a5097bd309e837a091fd1152bb1 Mon Sep 17 00:00:00 2001 From: LinXunFeng Date: Mon, 1 Nov 2021 11:21:36 +0800 Subject: [PATCH 1/3] Fix crash --- Sources/SwiftyImage/SwiftyImage.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftyImage/SwiftyImage.swift b/Sources/SwiftyImage/SwiftyImage.swift index ca9e32f..cdb6bcb 100644 --- a/Sources/SwiftyImage/SwiftyImage.swift +++ b/Sources/SwiftyImage/SwiftyImage.swift @@ -28,7 +28,9 @@ public extension UIImage { class func with(size: CGSize, opaque: Bool = false, scale: CGFloat = 0, block: ContextBlock) -> UIImage { UIGraphicsBeginImageContextWithOptions(size, opaque, scale) - let context = UIGraphicsGetCurrentContext()! + guard let context = UIGraphicsGetCurrentContext() else { + return UIImage() + } block(context) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() From 999d11a8a02ab720a2475ca12516f7c26470c3d8 Mon Sep 17 00:00:00 2001 From: LinXunFeng <598600855@qq.com> Date: Thu, 17 Feb 2022 10:21:06 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=20:=20=E8=B0=83=E6=95=B4=E9=A2=9C?= =?UTF-8?q?=E8=89=B2hashValue=E6=AF=94=E8=BE=83=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/SwiftyImage/SwiftyImage.swift | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftyImage/SwiftyImage.swift b/Sources/SwiftyImage/SwiftyImage.swift index cdb6bcb..1313b50 100644 --- a/Sources/SwiftyImage/SwiftyImage.swift +++ b/Sources/SwiftyImage/SwiftyImage.swift @@ -131,12 +131,12 @@ open class ImageDrawer { hasher.combine(UITraitCollection.current.userInterfaceStyle) } - hasher.combine(self.colors) + hasher.combine(self.colors.map({ $0.hexString() })) hasher.combine(self.colorLocations) hasher.combine(String(describing: self.colorStartPoint)) hasher.combine(String(describing: self.colorEndPoint)) - hasher.combine(self.borderColors) + hasher.combine(self.borderColors.map({ $0.hexString() })) hasher.combine(self.borderColorLocations) hasher.combine(String(describing: self.borderColorStartPoint)) hasher.combine(String(describing: self.borderColorEndPoint)) @@ -474,4 +474,19 @@ public extension UIImage { } } + +extension UIColor { + fileprivate func hexString() -> String { + let components = self.cgColor.components + let r: CGFloat = components?[0] ?? 0.0 + let g: CGFloat = components?[1] ?? 0.0 + let b: CGFloat = components?[2] ?? 0.0 + let a: CGFloat = components?[3] ?? 0.0 + + let hexString = String.init(format: "#%02lX%02lX%02lX%02lX", lroundf(Float(a * 255)), lroundf(Float(r * 255)), lroundf(Float(g * 255)), lroundf(Float(b * 255))) + print("hexString -- \(hexString)") + return hexString + } +} + #endif From fe490b0c8931607aab2b603a4b641e8281016461 Mon Sep 17 00:00:00 2001 From: LinXunFeng <598600855@qq.com> Date: Thu, 17 Feb 2022 11:14:04 +0800 Subject: [PATCH 3/3] : change implementation of hexString method --- Sources/SwiftyImage/SwiftyImage.swift | 36 ++++++++++++++++++++------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/Sources/SwiftyImage/SwiftyImage.swift b/Sources/SwiftyImage/SwiftyImage.swift index 1313b50..1e87969 100644 --- a/Sources/SwiftyImage/SwiftyImage.swift +++ b/Sources/SwiftyImage/SwiftyImage.swift @@ -477,15 +477,33 @@ public extension UIImage { extension UIColor { fileprivate func hexString() -> String { - let components = self.cgColor.components - let r: CGFloat = components?[0] ?? 0.0 - let g: CGFloat = components?[1] ?? 0.0 - let b: CGFloat = components?[2] ?? 0.0 - let a: CGFloat = components?[3] ?? 0.0 - - let hexString = String.init(format: "#%02lX%02lX%02lX%02lX", lroundf(Float(a * 255)), lroundf(Float(r * 255)), lroundf(Float(g * 255)), lroundf(Float(b * 255))) - print("hexString -- \(hexString)") - return hexString + var red: CGFloat = 0 + var green: CGFloat = 0 + var blue: CGFloat = 0 + var alpha: CGFloat = 0 + + let multiplier = CGFloat(255.999999) + + guard self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) else { + return "" + } + + if alpha == 1.0 { + return String( + format: "#%02lX%02lX%02lX", + Int(red * multiplier), + Int(green * multiplier), + Int(blue * multiplier) + ) + } else { + return String( + format: "#%02lX%02lX%02lX%02lX", + Int(red * multiplier), + Int(green * multiplier), + Int(blue * multiplier), + Int(alpha * multiplier) + ) + } } }