Skip to content

Commit e6d094b

Browse files
authored
Merge pull request #47 from SDWebImage/feature_convenient_placeholder_webimage
Add the convenient method for WebImage to directly supply SwiftUI.Image for placeholder
2 parents 5029a88 + 2936510 commit e6d094b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ var body: some View {
9090
// Success
9191
}
9292
.resizable() // Resizable like SwiftUI.Image
93+
.placeholder(Image(systemName: "photo")) // Placeholder Image
94+
// Supports ViewBuilder as well
9395
.placeholder {
94-
Image(systemName: "photo") // Placeholder
96+
Rectangle().foregroundColor(.gray)
9597
}
9698
.indicator(.activity) // Activity Indicator
9799
.animation(.easeInOut(duration: 0.5)) // Animation Duration
@@ -121,7 +123,7 @@ var body: some View {
121123
// Error
122124
}
123125
.resizable() // Actually this is not needed unlike SwiftUI.Image
124-
.placeholder(UIImage(systemName: "photo")) // Placeholder
126+
.placeholder(UIImage(systemName: "photo")) // Placeholder Image
125127
.indicator(SDWebImageActivityIndicator.medium) // Activity Indicator
126128
.transition(.fade) // Fade Transition
127129
.scaledToFit() // Attention to call it on AnimatedImage, but not `some View` after View Modifier
@@ -155,7 +157,7 @@ If you don't need animated image, prefer to use `WebImage` firstly. Which behave
155157

156158
If you need animated image, `AnimatedImage` is the one to choose. Remember it supports static image as well, you don't need to check the format, just use as it.
157159

158-
But, because `AnimatedImage` use `UIViewRepresentable` and driven by UIKit, currently there may be some small incompatible issues between UIKit and SwiftUI layout and animation system. We try our best to match SwiftUI behavior, and provide the same API as `WebImage`, which make it easy to switch between these two types.
160+
But, because `AnimatedImage` use `UIViewRepresentable` and driven by UIKit, currently there may be some small incompatible issues between UIKit and SwiftUI layout and animation system, or bugs related to SwiftUI itself. We try our best to match SwiftUI behavior, and provide the same API as `WebImage`, which make it easy to switch between these two types if needed.
159161

160162
For more information, it's really recommended to check our demo below, to learn detailed API usage.
161163

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ extension WebImage {
151151
return result
152152
}
153153

154+
/// Associate a placeholder image when loading image with url
155+
/// - note: This placeholder image will apply the same size and resizable from WebImage for convenience. If you don't want this, use the ViewBuilder one above instead
156+
/// - Parameter image: A Image view that describes the placeholder.
157+
public func placeholder(_ image: Image) -> WebImage {
158+
return placeholder {
159+
configurations.reduce(image) { (previous, configuration) in
160+
configuration(previous)
161+
}
162+
}
163+
}
164+
154165
/// Control the behavior to retry the failed loading when view become appears again
155166
/// - Parameter flag: Whether or not to retry the failed loading
156167
public func retryOnAppear(_ flag: Bool) -> WebImage {

0 commit comments

Comments
 (0)