@@ -16,9 +16,12 @@ public struct WebImage : View {
16
16
var options : SDWebImageOptions
17
17
var context : [ SDWebImageContextOption : Any ] ?
18
18
19
- var placeholder : AnyView ?
20
19
var configurations : [ ( Image ) -> Image ] = [ ]
21
20
21
+ var placeholder : AnyView ?
22
+ var retryOnAppear : Bool = true
23
+ var cancelOnDisappear : Bool = true
24
+
22
25
@ObservedObject var imageManager : ImageManager
23
26
24
27
/// Create a web image with url, placeholder, custom options and context.
@@ -50,11 +53,13 @@ public struct WebImage : View {
50
53
}
51
54
}
52
55
. onAppear {
56
+ guard self . retryOnAppear else { return }
53
57
if !self . imageManager. isFinished {
54
58
self . imageManager. load ( )
55
59
}
56
60
}
57
61
. onDisappear {
62
+ guard self . cancelOnDisappear else { return }
58
63
// When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case
59
64
if self . imageManager. isLoading && !self . imageManager. isIncremental {
60
65
self . imageManager. cancel ( )
@@ -133,7 +138,7 @@ extension WebImage {
133
138
}
134
139
}
135
140
136
- // Placeholder
141
+ // Custom Configuration
137
142
extension WebImage {
138
143
139
144
/// Associate a placeholder when loading image with url
@@ -144,6 +149,22 @@ extension WebImage {
144
149
result. placeholder = AnyView ( content ( ) )
145
150
return result
146
151
}
152
+
153
+ /// Control the behavior to retry the failed loading when view become appears again
154
+ /// - Parameter flag: Whether or not to retry the failed loading
155
+ public func retryOnAppear( _ flag: Bool ) -> WebImage {
156
+ var result = self
157
+ result. retryOnAppear = flag
158
+ return result
159
+ }
160
+
161
+ /// Control the behavior to cancel the pending loading when view become disappear again
162
+ /// - Parameter flag: Whether or not to cancel the pending loading
163
+ public func cancelOnDisappear( _ flag: Bool ) -> WebImage {
164
+ var result = self
165
+ result. cancelOnDisappear = flag
166
+ return result
167
+ }
147
168
}
148
169
149
170
// Indicator
0 commit comments