@@ -102,8 +102,17 @@ public struct WebImage : View {
102
102
}
103
103
104
104
public var body : some View {
105
- return Group {
106
- // Render Logic
105
+ // Container
106
+ return ZStack {
107
+ // This empty Image is used to receive container's level appear/disappear to start/stop player, reduce CPU usage
108
+ Image ( platformImage: . empty)
109
+ . onAppear {
110
+ self . appearAction ( )
111
+ }
112
+ . onDisappear {
113
+ self . disappearAction ( )
114
+ }
115
+ // Render Logic for actual animated image frame or static image
107
116
if imageManager. image != nil && imageModel. url == imageManager. currentURL {
108
117
if isAnimating && !imageManager. isIncremental {
109
118
setupPlayer ( )
@@ -136,8 +145,6 @@ public struct WebImage : View {
136
145
}
137
146
} )
138
147
}
139
- } . onDisappear {
140
- self . disappearAction ( )
141
148
}
142
149
}
143
150
@@ -207,18 +214,20 @@ public struct WebImage : View {
207
214
}
208
215
}
209
216
210
- /// Animated Image Disappear should stop display link
217
+ /// Container level to resume animation when appear
218
+ func appearAction( ) {
219
+ self . imagePlayer. startPlaying ( )
220
+ }
221
+
222
+ /// Container level to stop animation when disappear
211
223
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
- }
224
+ if self . imageConfiguration. pausable {
225
+ self . imagePlayer. pausePlaying ( )
226
+ } else {
227
+ self . imagePlayer. stopPlaying ( )
228
+ }
229
+ if self . imageConfiguration. purgeable {
230
+ self . imagePlayer. clearFrameBuffer ( )
222
231
}
223
232
}
224
233
@@ -235,19 +244,15 @@ public struct WebImage : View {
235
244
// Bind frame index to ID to ensure onDisappear called with sync
236
245
return configure ( image: currentFrame)
237
246
. id ( " \( imageModel. url!) : \( imagePlayer. currentFrameIndex) " )
238
- . onPlatformAppear ( appear: {
239
- self . imagePlayer. startPlaying ( )
240
- } , disappear: {
241
- disappearAction ( )
242
- } )
247
+ . onAppear { }
243
248
} else {
244
249
return configure ( image: imageManager. image!)
245
250
. id ( " \( imageModel. url!) : \( imagePlayer. currentFrameIndex) " )
246
- . onPlatformAppear ( appear : {
251
+ . onAppear {
247
252
if shouldResetPlayer {
248
253
// Clear previous status
249
254
self . imagePlayer. stopPlaying ( )
250
- self . imagePlayer. player = nil ;
255
+ self . imagePlayer. player = nil
251
256
self . imagePlayer. currentFrame = nil ;
252
257
self . imagePlayer. currentFrameIndex = 0 ;
253
258
self . imagePlayer. currentLoopCount = 0 ;
@@ -262,9 +267,7 @@ public struct WebImage : View {
262
267
self . imagePlayer. setupPlayer ( animatedImage: animatedImage)
263
268
self . imagePlayer. startPlaying ( )
264
269
}
265
- } , disappear: {
266
- disappearAction ( )
267
- } )
270
+ }
268
271
}
269
272
}
270
273
0 commit comments