-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add ImageSize #23929
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
Add ImageSize #23929
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,8 +28,8 @@ public final class ImageRequest: Sendable { | |
| } | ||
|
|
||
| public struct ImageRequestOptions: Hashable, Sendable { | ||
| /// Resize the thumbnail to the given size (in pixels). By default, `nil`. | ||
| public var size: CGSize? | ||
| /// Resize the thumbnail to the given size. By default, `nil`. | ||
| public var size: ImageSize? | ||
|
|
||
| /// If enabled, uses ``MemoryCache`` for caching decompressed images. | ||
| public var isMemoryCacheEnabled = true | ||
|
|
@@ -39,7 +39,7 @@ public struct ImageRequestOptions: Hashable, Sendable { | |
| public var isDiskCacheEnabled = true | ||
|
|
||
| public init( | ||
| size: CGSize? = nil, | ||
| size: ImageSize? = nil, | ||
| isMemoryCacheEnabled: Bool = true, | ||
| isDiskCacheEnabled: Bool = true | ||
| ) { | ||
|
|
@@ -48,3 +48,37 @@ public struct ImageRequestOptions: Hashable, Sendable { | |
| self.isDiskCacheEnabled = isDiskCacheEnabled | ||
| } | ||
| } | ||
|
|
||
| /// Image size in **pixels**. | ||
| public struct ImageSize: Hashable, Sendable { | ||
| public let width: CGFloat | ||
| public let height: CGFloat | ||
|
|
||
| public init(width: CGFloat, height: CGFloat) { | ||
| self.width = width | ||
| self.height = height | ||
| } | ||
|
|
||
| public init(_ size: CGSize) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you thinking changing this API to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense; updating in the upcoming PR. |
||
| self.width = size.width | ||
| self.height = size.height | ||
| } | ||
|
|
||
| /// Initializes `ImageSize` with the given size scaled for the given view. | ||
| @MainActor | ||
| public init(scaling size: CGSize, in view: UIView) { | ||
| self.init(size.scaled(by: view.traitCollection.displayScale)) | ||
| } | ||
|
|
||
| /// Initializes `ImageSize` with the given size scaled for the current trait | ||
| /// collection display scale. | ||
| public init(scaling size: CGSize) { | ||
| self.init(size.scaled(by: UITraitCollection.current.displayScale)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Of course, you can add some convenient methods on top of it, like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you can misuse /// A convenience initializer that creates `ImageSize` with the given size
/// in **points** scaled for the given view.
@MainActor
public init(scaling size: CGSize, in view: UIView) {
self.init(scaling: size, scale: view.traitCollection.displayScale)
}
/// Initializes `ImageSize` with the given size in **points** scaled for the
/// current trait collection display scale.
public init(scaling size: CGSize, scale: CGFloat) {
self.init(pixels: size.scaled(by: max(1, scale)))
}
|
||
| } | ||
| } | ||
|
|
||
| extension CGSize { | ||
| init(_ size: ImageSize) { | ||
| self.init(width: size.width, height: size.height) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these properties be Int? There is no decimal pixel, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in the upcoming PR.