diff --git a/SDWebImageSwiftUI/Classes/WebImage.swift b/SDWebImageSwiftUI/Classes/WebImage.swift index 6e0c3f34..b58a0131 100644 --- a/SDWebImageSwiftUI/Classes/WebImage.swift +++ b/SDWebImageSwiftUI/Classes/WebImage.swift @@ -52,29 +52,30 @@ public struct WebImage : View { } self.imageManager = ImageManager(url: url, options: options, context: context) self.imagePlayer = ImagePlayer() - } - - public var body: some View { + // This solve the case when WebImage created with new URL, but `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :) if imageManager.isFirstLoad { imageManager.load() } + } + + public var body: some View { return Group { if let image = imageManager.image { if isAnimating && !imageManager.isIncremental { setupPlayer() - .onPlatformAppear(appear: { - self.imagePlayer.startPlaying() - }, disappear: { - if self.pausable { - self.imagePlayer.pausePlaying() - } else { - self.imagePlayer.stopPlaying() + .onAppear { + self.imagePlayer.startPlaying() + }.onDisappear { + if self.pausable { + self.imagePlayer.pausePlaying() + } else { + self.imagePlayer.stopPlaying() + } + if self.purgeable { + self.imagePlayer.clearFrameBuffer() + } } - if self.purgeable { - self.imagePlayer.clearFrameBuffer() - } - }) } else { if let currentFrame = imagePlayer.currentFrame { configure(image: currentFrame) @@ -84,24 +85,24 @@ public struct WebImage : View { } } else { setupPlaceholder() - .onPlatformAppear(appear: { - // Load remote image when first appear - if self.imageManager.isFirstLoad { - self.imageManager.load() - return - } - guard self.retryOnAppear else { return } - // When using prorgessive loading, the new partial image will cause onAppear. Filter this case - if self.imageManager.image == nil && !self.imageManager.isIncremental { - self.imageManager.load() - } - }, disappear: { - guard self.cancelOnDisappear else { return } - // When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case - if self.imageManager.image == nil && !self.imageManager.isIncremental { - self.imageManager.cancel() + .onAppear { + // Load remote image when first appear + if self.imageManager.isFirstLoad { + self.imageManager.load() + return + } + guard self.retryOnAppear else { return } + // When using prorgessive loading, the new partial image will cause onAppear. Filter this case + if self.imageManager.image == nil && !self.imageManager.isIncremental { + self.imageManager.load() + } + }.onDisappear { + guard self.cancelOnDisappear else { return } + // When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case + if self.imageManager.image == nil && !self.imageManager.isIncremental { + self.imageManager.cancel() + } } - }) } } } @@ -158,13 +159,14 @@ public struct WebImage : View { /// Animated Image Support func setupPlayer() -> some View { if let currentFrame = imagePlayer.currentFrame { - return configure(image: currentFrame) + return configure(image: currentFrame).onAppear() } else { - if let animatedImage = imageManager.image as? SDAnimatedImageProvider { - self.imagePlayer.setupPlayer(animatedImage: animatedImage) - self.imagePlayer.startPlaying() + return configure(image: imageManager.image!).onAppear { + if let animatedImage = imageManager.image as? SDAnimatedImageProvider { + self.imagePlayer.setupPlayer(animatedImage: animatedImage) + self.imagePlayer.startPlaying() + } } - return configure(image: imageManager.image!) } }