diff --git a/ADUtilsTests/UIImageColorTests.swift b/ADUtilsTests/UIImageColorTests.swift index 6b2301e..f98296b 100644 --- a/ADUtilsTests/UIImageColorTests.swift +++ b/ADUtilsTests/UIImageColorTests.swift @@ -40,6 +40,22 @@ class UIImageColorTests: QuickSpec { assertSnapshot(matching: imageView, as: .image, named: "UIImageColorRedPixel") } + it("should have correct snapshot with scale") { + // Given + let size = CGSize(width: 10, height: 20) + let scale: CGFloat = 3.0 + let color = UIColor.red + + // When + let image = UIImage.ad_filled(with: color, size: size, scale: scale) + + // Then + XCTAssertEqual(image?.scale, 3.0) + XCTAssertEqual(image?.size, size) + let imageView = UIImageView(image: image) + assertSnapshot(matching: imageView, as: .image(traits: UITraitCollection(displayScale: scale)), named: "UIImageColorRedWithScale") + } + if #available(iOS 13.0, *) { it("should create images for light and dark modes") { diff --git a/ADUtilsTests/__Snapshots__/UIImageColorTests/spec.UIImageColorRedWithScale.png b/ADUtilsTests/__Snapshots__/UIImageColorTests/spec.UIImageColorRedWithScale.png new file mode 100644 index 0000000..c6224d6 Binary files /dev/null and b/ADUtilsTests/__Snapshots__/UIImageColorTests/spec.UIImageColorRedWithScale.png differ diff --git a/CHANGELOG.md b/CHANGELOG.md index 456b7b6..1885348 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file. ### Created - Add a SwiftUI Font provider in `DynamicFont` +- Add scale parameter to `UIImage.ad_filled`. ## [11.3.0] - 2022-08-01Z diff --git a/Modules/ADUtils/UIImage+Color.swift b/Modules/ADUtils/UIImage+Color.swift index 6121447..7578fca 100644 --- a/Modules/ADUtils/UIImage+Color.swift +++ b/Modules/ADUtils/UIImage+Color.swift @@ -16,16 +16,19 @@ public extension UIImage { - parameter size: The size of the final image */ static func ad_filled(with color: UIColor, - size: CGSize = CGSize(width: 1, height: 1)) -> UIImage? { + size: CGSize = CGSize(width: 1, height: 1), + scale: CGFloat = 1.0) -> UIImage? { if #available(iOS 13.0, tvOS 13.0, *) { let lightModeImage = generateImage( withColor: color, size: size, + scale: scale, userInterfaceStyle: .light ) let darkModeImage = generateImage( withColor: color, size: size, + scale: scale, userInterfaceStyle: .dark ) if let darkImage = darkModeImage { @@ -36,7 +39,7 @@ public extension UIImage { } return lightModeImage } else { - return generateImage(withColor: color, size: size) + return generateImage(withColor: color, size: size, scale: scale) } } @@ -45,23 +48,23 @@ public extension UIImage { @available(iOS 13.0, tvOS 13.0, *) private static func generateImage(withColor color: UIColor, size: CGSize, + scale: CGFloat, userInterfaceStyle: UIUserInterfaceStyle) -> UIImage? { var image: UIImage? - UITraitCollection(userInterfaceStyle: userInterfaceStyle).performAsCurrent { - image = generateImage(withColor: color, size: size) - } + UITraitCollection(userInterfaceStyle: userInterfaceStyle).performAsCurrent { + image = generateImage(withColor: color, size: size, scale: scale) + } return image } private static func generateImage(withColor color: UIColor, - size: CGSize) -> UIImage? { - let rect = CGRect(origin: .zero, size: size) - UIGraphicsBeginImageContext(rect.size) - let context = UIGraphicsGetCurrentContext() - context?.setFillColor(color.cgColor) - context?.fill(rect) - let image = UIGraphicsGetImageFromCurrentImageContext() - UIGraphicsEndImageContext() - return image + size: CGSize, + scale: CGFloat) -> UIImage? { + let format = UIGraphicsImageRendererFormat() + format.scale = scale + return UIGraphicsImageRenderer(size: size, format: format).image { context in + context.cgContext.setFillColor(color.cgColor) + context.fill(CGRect(origin: .zero, size: size)) + } } }