Skip to content

Add the convenient method for WebImage to directly supply SwiftUI.Image for placeholder #47

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

Merged
merged 2 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ var body: some View {
// Success
}
.resizable() // Resizable like SwiftUI.Image
.placeholder(Image(systemName: "photo")) // Placeholder Image
// Supports ViewBuilder as well
.placeholder {
Image(systemName: "photo") // Placeholder
Rectangle().foregroundColor(.gray)
}
.indicator(.activity) // Activity Indicator
.animation(.easeInOut(duration: 0.5)) // Animation Duration
Expand Down Expand Up @@ -121,7 +123,7 @@ var body: some View {
// Error
}
.resizable() // Actually this is not needed unlike SwiftUI.Image
.placeholder(UIImage(systemName: "photo")) // Placeholder
.placeholder(UIImage(systemName: "photo")) // Placeholder Image
.indicator(SDWebImageActivityIndicator.medium) // Activity Indicator
.transition(.fade) // Fade Transition
.scaledToFit() // Attention to call it on AnimatedImage, but not `some View` after View Modifier
Expand Down Expand Up @@ -155,7 +157,7 @@ If you don't need animated image, prefer to use `WebImage` firstly. Which behave

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.

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.
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.

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

Expand Down
11 changes: 11 additions & 0 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ extension WebImage {
return result
}

/// Associate a placeholder image when loading image with url
/// - 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
/// - Parameter image: A Image view that describes the placeholder.
public func placeholder(_ image: Image) -> WebImage {
return placeholder {
configurations.reduce(image) { (previous, configuration) in
configuration(previous)
}
}
}

/// Control the behavior to retry the failed loading when view become appears again
/// - Parameter flag: Whether or not to retry the failed loading
public func retryOnAppear(_ flag: Bool) -> WebImage {
Expand Down