Skip to content

Commit 03c468b

Browse files
authored
Merge pull request #323 from Kyle-Ye/feature/kyle/scale
Add Image scale support in WebImage init
2 parents 9cf2f51 + ecce423 commit 03c468b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

SDWebImageSwiftUI/Classes/WebImage.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,17 @@ public struct WebImage<Content> : View where Content: View {
108108

109109
/// Create a web image with url, placeholder, custom options and context. Optional can support animated image using Binding.
110110
/// - Parameter url: The image url
111+
/// - Parameter scale: The scale to use for the image. The default is 1. Set a different value when loading images designed for higher resolution displays. For example, set a value of 2 for an image that you would name with the @2x suffix if stored in a file on disk.
111112
/// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values.
112113
/// - Parameter context: A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
113114
/// - Parameter isAnimating: The binding for animation control. The binding value should be `true` when initialized to setup the correct animated image class. If not, you must provide the `.animatedImageClass` explicitly. When the animation started, this binding can been used to start / stop the animation.
114-
public init(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool> = .constant(true)) where Content == Image {
115+
public init(url: URL?, scale: CGFloat = 1, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool> = .constant(true)) where Content == Image {
115116
self.init(url: url, options: options, context: context, isAnimating: isAnimating) { phase in
116117
phase.image ?? Image(platformImage: .empty)
117118
}
118119
}
119120

120-
public init<I, P>(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool> = .constant(true), @ViewBuilder content: @escaping (Image) -> I, @ViewBuilder placeholder: @escaping () -> P) where Content == _ConditionalContent<I, P>, I: View, P: View {
121+
public init<I, P>(url: URL?, scale: CGFloat = 1, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool> = .constant(true), @ViewBuilder content: @escaping (Image) -> I, @ViewBuilder placeholder: @escaping () -> P) where Content == _ConditionalContent<I, P>, I: View, P: View {
121122
self.init(url: url, options: options, context: context, isAnimating: isAnimating) { phase in
122123
if let i = phase.image {
123124
content(i)
@@ -127,9 +128,12 @@ public struct WebImage<Content> : View where Content: View {
127128
}
128129
}
129130

130-
public init(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool> = .constant(true), transaction: Transaction = Transaction(), @ViewBuilder content: @escaping (WebImagePhase) -> Content) {
131+
public init(url: URL?, scale: CGFloat = 1, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool> = .constant(true), transaction: Transaction = Transaction(), @ViewBuilder content: @escaping (WebImagePhase) -> Content) {
131132
self._isAnimating = isAnimating
132133
var context = context ?? [:]
134+
if context[.imageScaleFactor] == nil {
135+
context[.imageScaleFactor] = scale
136+
}
133137
// provide animated image class if the initialized `isAnimating` is true, user can still custom the image class if they want
134138
if isAnimating.wrappedValue {
135139
if context[.animatedImageClass] == nil {

0 commit comments

Comments
 (0)