@@ -17,6 +17,8 @@ public struct WebImage : View {
1717 var placeholder : AnyView ?
1818 var retryOnAppear : Bool = true
1919 var cancelOnDisappear : Bool = true
20+ var pausable : Bool = true
21+ var purgeable : Bool = false
2022
2123 @ObservedObject var imageManager : ImageManager
2224
@@ -26,9 +28,6 @@ public struct WebImage : View {
2628
2729 @ObservedObject var imagePlayer : ImagePlayer
2830
29- var pausable : Bool = true
30- var purgeable : Bool = false
31-
3231 /// Create a web image with url, placeholder, custom options and context.
3332 /// - Parameter url: The image url
3433 /// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values.
@@ -61,38 +60,30 @@ public struct WebImage : View {
6160 imageManager. load ( )
6261 }
6362 return Group {
64- if imageManager . image != nil {
63+ if let image = imageManager . image {
6564 if isAnimating && !imageManager. isIncremental {
66- if imagePlayer. currentFrame != nil {
67- configure ( image: imagePlayer. currentFrame!)
68- . onPlatformAppear ( appear: {
69- self . imagePlayer. startPlaying ( )
70- } , disappear: {
71- if self . pausable {
72- self . imagePlayer. pausePlaying ( )
73- } else {
74- self . imagePlayer. stopPlaying ( )
75- }
76- if self . purgeable {
77- self . imagePlayer. clearFrameBuffer ( )
78- }
79- } )
80- } else {
81- configure ( image: imageManager. image!)
82- . onReceive ( imageManager. $image) { image in
83- self . imagePlayer. setupPlayer ( image: image)
65+ setupPlayer ( )
66+ . onPlatformAppear ( appear: {
67+ self . imagePlayer. startPlaying ( )
68+ } , disappear: {
69+ if self . pausable {
70+ self . imagePlayer. pausePlaying ( )
71+ } else {
72+ self . imagePlayer. stopPlaying ( )
8473 }
85- }
74+ if self . purgeable {
75+ self . imagePlayer. clearFrameBuffer ( )
76+ }
77+ } )
8678 } else {
87- if imagePlayer . currentFrame != nil {
88- configure ( image: imagePlayer . currentFrame! )
79+ if let currentFrame = imagePlayer . currentFrame {
80+ configure ( image: currentFrame)
8981 } else {
90- configure ( image: imageManager . image! )
82+ configure ( image: image)
9183 }
9284 }
9385 } else {
9486 setupPlaceholder ( )
95- . frame ( minWidth: 0 , maxWidth: . infinity, minHeight: 0 , maxHeight: . infinity)
9687 . onPlatformAppear ( appear: {
9788 // Load remote image when first appear
9889 if self . imageManager. isFirstLoad {
@@ -164,6 +155,19 @@ public struct WebImage : View {
164155 }
165156 }
166157
158+ /// Animated Image Support
159+ func setupPlayer( ) -> some View {
160+ if let currentFrame = imagePlayer. currentFrame {
161+ return configure ( image: currentFrame)
162+ } else {
163+ if let animatedImage = imageManager. image as? SDAnimatedImageProvider {
164+ self . imagePlayer. setupPlayer ( animatedImage: animatedImage)
165+ self . imagePlayer. startPlaying ( )
166+ }
167+ return configure ( image: imageManager. image!)
168+ }
169+ }
170+
167171 /// Placeholder View Support
168172 func setupPlaceholder( ) -> some View {
169173 // Don't use `Group` because it will trigger `.onAppear` and `.onDisappear` when condition view removed, treat placeholder as an entire component
0 commit comments