@@ -22,7 +22,10 @@ public struct WebImage : View {
22
22
var retryOnAppear : Bool = true
23
23
var cancelOnDisappear : Bool = true
24
24
25
+ @State var currentFrame : PlatformImage ? = nil
26
+
25
27
@ObservedObject var imageManager : ImageManager
28
+ var imagePlayer : SDAnimatedImagePlayer ?
26
29
27
30
/// Create a web image with url, placeholder, custom options and context.
28
31
/// - Parameter url: The image url
@@ -31,6 +34,8 @@ public struct WebImage : View {
31
34
public init ( url: URL ? , options: SDWebImageOptions = [ ] , context: [ SDWebImageContextOption : Any ] ? = nil ) {
32
35
self . url = url
33
36
self . options = options
37
+ var context = context ?? [ : ]
38
+ context [ . animatedImageClass] = SDAnimatedImage . self
34
39
self . context = context
35
40
self . imageManager = ImageManager ( url: url, options: options, context: context)
36
41
// load remote image here, SwiftUI sometimes will create a new View struct without calling `onAppear` (like enter EditMode) :)
@@ -41,8 +46,12 @@ public struct WebImage : View {
41
46
public var body : some View {
42
47
Group {
43
48
if imageManager. image != nil {
44
- configurations. reduce ( Image ( platformImage: imageManager. image!) ) { ( previous, configuration) in
45
- configuration ( previous)
49
+ if currentFrame != nil {
50
+ configurations. reduce ( Image ( platformImage: currentFrame!) ) { ( previous, configuration) in
51
+ configuration ( previous)
52
+ }
53
+ } else {
54
+ queryFrames ( )
46
55
}
47
56
} else {
48
57
Group {
@@ -69,6 +78,21 @@ public struct WebImage : View {
69
78
}
70
79
}
71
80
}
81
+
82
+ func queryFrames( ) -> some View {
83
+ if ( imageManager. image as? SDAnimatedImageProvider ) != nil {
84
+ let imagePlayer = SDAnimatedImagePlayer ( provider: ( imageManager. image as! SDAnimatedImageProvider ) )
85
+ var result = self
86
+ imagePlayer? . animationFrameHandler = { ( _, frame) in
87
+ result. currentFrame = frame
88
+ }
89
+ imagePlayer? . startPlaying ( )
90
+ result. imagePlayer = imagePlayer
91
+ }
92
+ return configurations. reduce ( Image ( platformImage: imageManager. image!) ) { ( previous, configuration) in
93
+ configuration ( previous)
94
+ }
95
+ }
72
96
}
73
97
74
98
// Layout
0 commit comments