From c9c35d0281bdd5b51d433e4c69cca47106b280c4 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 9 Nov 2019 17:05:07 +0800 Subject: [PATCH 1/2] Add the convenient method for WebImage to directly supply SwiftUI.Image for placeholder --- SDWebImageSwiftUI/Classes/WebImage.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/SDWebImageSwiftUI/Classes/WebImage.swift b/SDWebImageSwiftUI/Classes/WebImage.swift index e3804bc3..ee9c42ce 100644 --- a/SDWebImageSwiftUI/Classes/WebImage.swift +++ b/SDWebImageSwiftUI/Classes/WebImage.swift @@ -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 { From 2936510f703d137ba077ddb2cd92b6160d39f61f Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 9 Nov 2019 17:12:32 +0800 Subject: [PATCH 2/2] Update the readme about the placeholder convenient API --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index afb178d5..3a2b6315 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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.