Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improve image generation from color #42

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

par défaut, ça vaut pas le scale du device ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai préféré laisser 1.0 par défaut pour ne pas changer le comportement de base de la méthode. Mais a l'utilisation oui on voudra utiliser le scale du device si on a besoin d'autre chose qu'un rectangle de couleur

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Justement, vu qu'on précise dorénavant un scale, je me demande si on n'a pas changé le comportement actuel

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avant ce changement l'image était générée avec un scale de 1, et tous les anciens tests passent encore, donc je pense que l'ancien comportement a bien été conservé

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on peut faut juste prévenir les gens
du coup sans majeure, soit mineure
et dans tous les cas mettre une gros attention
meme si en théorie la scale en plus devrait juste améliorer l'existant partout

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avant ce changement l'image était générée avec un scale de 1, et tous les anciens tests passent encore, donc je pense que l'ancien comportement a bien été conservé

y'a moyen que dans les tests y'avait pas la scale non?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avant ce changement l'image était générée avec un scale de 1, et tous les anciens tests passent encore, donc je pense que l'ancien comportement a bien été conservé

y'a moyen que dans les tests y'avait pas la scale non?

Le scale n'est pas fournis pour les anciens tests et pourtant ils sont sur un simu qui a plus de x1.

Le problème, c'est que UIScreen.main est déprécié, et donc je ne suis pas trop fan de le mettre en valeur par défaut de la méthode (A moins qu'il y ait un autre moyen d'avoir l'info ?). Après je peux déprécier l'appel à ad_filled sans le scale, mais je pense pas qu'on ait besoin en général du scale sur des images de couleur

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si j'en crois la doc, on est bon, c'était un scale de 1.0 😌

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je parlais de vérification sur le scale pour les tests ;)
yes ils était justement tous à un par défaut

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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))
}
}
}
Loading