I use this so often I’ve added it to my global utilities class. Seriously considering taking all my extensions open source – since I use in my utilities class at all clients.
extension UIColor { public func uiImage(_ size: CGSize = CGSize(width: 0.1, height: 0.1)) -> UIImage { UIGraphicsImageRenderer(size: size).image { rendererContext in self.setFill() rendererContext.fill(CGRect(origin: .zero, size: size)) } } /// creates a cgImage from this color /// - Parameter size: (CGSize) the size of the image /// - Returns: cgImage public func cgImage(_ size: CGSize = CGSize(width: 0.1, height: 0.1)) -> CGImage { self.uiImage(size).cgImage! } /// creates a square Swift Image with this color as background /// - Parameters: /// - size: size of the image to be created /// - label: text to dispaly within the image /// - Returns: cgImage public func swiftImage(_ size: CGSize = CGSize(width: 0.1, height: 0.1), _ label: String = String.empty) -> Image { Image(self.cgImage(size), scale: CGFloat(2), label: Text(verbatim: label)) } /// creates a Swift Color from this UIColor /// - Returns: Swift Color public func swiftColor() -> Color { Color(self) } }