diff --git a/Sources/SwiftyImage/SwiftyImage.swift b/Sources/SwiftyImage/SwiftyImage.swift index ca9e32f..1e87969 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() @@ -129,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)) @@ -472,4 +474,37 @@ public extension UIImage { } } + +extension UIColor { + fileprivate func hexString() -> String { + 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) + ) + } + } +} + #endif