@@ -118,7 +118,7 @@ public struct WebImage : View {
118
118
// Load Logic
119
119
setupPlaceholder ( )
120
120
. onPlatformAppear ( appear: {
121
- setupManager ( )
121
+ self . setupManager ( )
122
122
if ( self . imageManager. error == nil ) {
123
123
// Load remote image when first appear
124
124
self . imageManager. load ( url: imageModel. url, options: imageModel. options, context: imageModel. context)
@@ -136,6 +136,8 @@ public struct WebImage : View {
136
136
}
137
137
} )
138
138
}
139
+ } . onDisappear {
140
+ self . disappearAction ( )
139
141
}
140
142
}
141
143
@@ -205,36 +207,52 @@ public struct WebImage : View {
205
207
}
206
208
}
207
209
210
+ /// Animated Image Disappear should stop display link
211
+ func disappearAction( ) {
212
+ // Only stop the player which is not intermediate status
213
+ if !imagePlayer. isWaiting {
214
+ if self . imageConfiguration. pausable {
215
+ self . imagePlayer. pausePlaying ( )
216
+ } else {
217
+ self . imagePlayer. stopPlaying ( )
218
+ }
219
+ if self . imageConfiguration. purgeable {
220
+ self . imagePlayer. clearFrameBuffer ( )
221
+ }
222
+ }
223
+ }
224
+
208
225
/// Animated Image Support
209
226
func setupPlayer( ) -> some View {
210
- let disappearAction = {
211
- // Only stop the player which is not intermediate status
212
- if !imagePlayer. isWaiting {
213
- if self . imageConfiguration. pausable {
214
- self . imagePlayer. pausePlaying ( )
215
- } else {
216
- self . imagePlayer. stopPlaying ( )
217
- }
218
- if self . imageConfiguration. purgeable {
219
- self . imagePlayer. clearFrameBuffer ( )
220
- }
221
- }
227
+ let shouldResetPlayer : Bool
228
+ // Image compare should use ===/!==, which is faster than isEqual:
229
+ if let animatedImage = imagePlayer. currentAnimatedImage, animatedImage !== imageManager. image! {
230
+ shouldResetPlayer = true
231
+ } else {
232
+ shouldResetPlayer = false
222
233
}
223
- if let currentFrame = imagePlayer. currentFrame, imagePlayer. currentAnimatedImage == imageManager. image! {
224
- return configure ( image: currentFrame) . onPlatformAppear ( appear: {
234
+ if let currentFrame = imagePlayer. currentFrame, !shouldResetPlayer {
235
+ // Bind frame index to ID to ensure onDisappear called with sync
236
+ return configure ( image: currentFrame)
237
+ . id ( " \( imageModel. url!) : \( imagePlayer. currentFrameIndex) " )
238
+ . onPlatformAppear ( appear: {
225
239
self . imagePlayer. startPlaying ( )
226
240
} , disappear: {
227
241
disappearAction ( )
228
242
} )
229
243
} else {
230
- return configure ( image: imageManager. image!) . onPlatformAppear ( appear: {
231
- self . imagePlayer. stopPlaying ( )
232
- if let animatedImage = imageManager. image as? PlatformImage & SDAnimatedImageProvider {
244
+ return configure ( image: imageManager. image!)
245
+ . id ( " \( imageModel. url!) : \( imagePlayer. currentFrameIndex) " )
246
+ . onPlatformAppear ( appear: {
247
+ if shouldResetPlayer {
233
248
// Clear previous status
249
+ self . imagePlayer. stopPlaying ( )
234
250
self . imagePlayer. player = nil ;
235
251
self . imagePlayer. currentFrame = nil ;
236
252
self . imagePlayer. currentFrameIndex = 0 ;
237
253
self . imagePlayer. currentLoopCount = 0 ;
254
+ }
255
+ if let animatedImage = imageManager. image as? PlatformImage & SDAnimatedImageProvider {
238
256
self . imagePlayer. customLoopCount = self . imageConfiguration. customLoopCount
239
257
self . imagePlayer. maxBufferSize = self . imageConfiguration. maxBufferSize
240
258
self . imagePlayer. runLoopMode = self . imageConfiguration. runLoopMode
0 commit comments