Skip to content

Commit

Permalink
Merge pull request #42 from faberNovel/feature/improve_image_generati…
Browse files Browse the repository at this point in the history
…on_from_color

Feature/improve image generation from color
  • Loading branch information
alexandre-pod authored Sep 19, 2023
2 parents 602b271 + 9d85884 commit e334696
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
16 changes: 16 additions & 0 deletions ADUtilsTests/UIImageColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Updated

- Add scale parameter to `UIImage.ad_filled`.

## [11.4.0] - 2023-07-04

### Created
Expand Down
8 changes: 8 additions & 0 deletions MAINTAINER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

## How to release a version

- create & push a release branch `release/vA.B.C` -> the prepare_release action will automatically create the PR to merge this release into master.
- Update version in podfile & run pod install to update podfile
- update changelog
- merge PR -> the CI action will trigger a pod release and tag the commit
- once the PR is merged, merge the branch `release/vA.B.C` into master
31 changes: 17 additions & 14 deletions Modules/ADUtils/UIImage+Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,7 +39,7 @@ public extension UIImage {
}
return lightModeImage
} else {
return generateImage(withColor: color, size: size)
return generateImage(withColor: color, size: size, scale: scale)
}
}

Expand All @@ -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))
}
}
}

0 comments on commit e334696

Please sign in to comment.