@@ -102,8 +102,17 @@ public struct WebImage : View {
102102 }
103103
104104 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
107116 if imageManager. image != nil && imageModel. url == imageManager. currentURL {
108117 if isAnimating && !imageManager. isIncremental {
109118 setupPlayer ( )
@@ -136,8 +145,6 @@ public struct WebImage : View {
136145 }
137146 } )
138147 }
139- } . onDisappear {
140- self . disappearAction ( )
141148 }
142149 }
143150
@@ -207,18 +214,20 @@ public struct WebImage : View {
207214 }
208215 }
209216
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
211223 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 ( )
222231 }
223232 }
224233
@@ -235,19 +244,15 @@ public struct WebImage : View {
235244 // Bind frame index to ID to ensure onDisappear called with sync
236245 return configure ( image: currentFrame)
237246 . id ( " \( imageModel. url!) : \( imagePlayer. currentFrameIndex) " )
238- . onPlatformAppear ( appear: {
239- self . imagePlayer. startPlaying ( )
240- } , disappear: {
241- disappearAction ( )
242- } )
247+ . onAppear { }
243248 } else {
244249 return configure ( image: imageManager. image!)
245250 . id ( " \( imageModel. url!) : \( imagePlayer. currentFrameIndex) " )
246- . onPlatformAppear ( appear : {
251+ . onAppear {
247252 if shouldResetPlayer {
248253 // Clear previous status
249254 self . imagePlayer. stopPlaying ( )
250- self . imagePlayer. player = nil ;
255+ self . imagePlayer. player = nil
251256 self . imagePlayer. currentFrame = nil ;
252257 self . imagePlayer. currentFrameIndex = 0 ;
253258 self . imagePlayer. currentLoopCount = 0 ;
@@ -262,9 +267,7 @@ public struct WebImage : View {
262267 self . imagePlayer. setupPlayer ( animatedImage: animatedImage)
263268 self . imagePlayer. startPlaying ( )
264269 }
265- } , disappear: {
266- disappearAction ( )
267- } )
270+ }
268271 }
269272 }
270273
0 commit comments