diff --git a/SDWebImageSwiftUI/Classes/ImageManager.swift b/SDWebImageSwiftUI/Classes/ImageManager.swift index 6a752c29..df80d7d3 100644 --- a/SDWebImageSwiftUI/Classes/ImageManager.swift +++ b/SDWebImageSwiftUI/Classes/ImageManager.swift @@ -19,6 +19,7 @@ class ImageManager : ObservableObject { weak var currentOperation: SDWebImageOperation? = nil var isSuccess: Bool = false // true means request for this URL is ended forever, load() do nothing var isIncremental: Bool = false // true means during incremental loading + var isFirstLoad: Bool = true // false after first call `load()` var url: URL? var options: SDWebImageOptions @@ -39,6 +40,7 @@ class ImageManager : ObservableObject { } func load() { + isFirstLoad = false if currentOperation != nil { return } diff --git a/SDWebImageSwiftUI/Classes/WebImage.swift b/SDWebImageSwiftUI/Classes/WebImage.swift index f76aab41..c533ccee 100644 --- a/SDWebImageSwiftUI/Classes/WebImage.swift +++ b/SDWebImageSwiftUI/Classes/WebImage.swift @@ -32,13 +32,16 @@ public struct WebImage : View { /// - 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. public init(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) { self.imageManager = ImageManager(url: url, options: options, context: context) - // load remote image here, SwiftUI sometimes will create a new View struct without calling `onAppear` (like enter EditMode) :) - // this can ensure we load the image, SDWebImage take care of the duplicated query - self.imageManager.load() } public var body: some View { - Group { + // load remote image when first called `body`, SwiftUI sometimes will create a new View struct without calling `onAppear` (like enter EditMode) :) + // this can ensure we load the image, and display image synchronously when memory cache hit to avoid flashing + // called once per struct, SDWebImage take care of the duplicated query + if imageManager.isFirstLoad { + imageManager.load() + } + return Group { if imageManager.image != nil { if animated { if currentFrame != nil {