@@ -12,7 +12,6 @@ import SDWebImage
12
12
// Data Binding Object
13
13
final class AnimatedImageModel : ObservableObject {
14
14
@Published var image : PlatformImage ?
15
- @Published var url : URL ?
16
15
@Published var successBlock : ( ( PlatformImage , SDImageCacheType ) -> Void ) ?
17
16
@Published var failureBlock : ( ( Error ) -> Void ) ?
18
17
@Published var progressBlock : ( ( Int , Int ) -> Void ) ?
@@ -52,6 +51,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
52
51
@ObservedObject var imageLayout = AnimatedImageLayout ( )
53
52
@ObservedObject var imageConfiguration = AnimatedImageConfiguration ( )
54
53
54
+ var url : URL ?
55
55
var placeholder : PlatformImage ?
56
56
var webOptions : SDWebImageOptions = [ ]
57
57
var webContext : [ SDWebImageContextOption : Any ] ? = nil
@@ -80,7 +80,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
80
80
self . placeholder = placeholder
81
81
self . webOptions = options
82
82
self . webContext = context
83
- self . imageModel . url = url
83
+ self . url = url
84
84
}
85
85
86
86
/// Create an animated image with name and bundle.
@@ -174,19 +174,23 @@ public struct AnimatedImage : PlatformViewRepresentable {
174
174
}
175
175
176
176
func updateView( _ view: AnimatedImageViewWrapper , context: PlatformViewRepresentableContext < AnimatedImage > ) {
177
- #if os(watchOS)
178
- view. wrapped. setImage ( imageModel. image)
179
- #else
180
- view. wrapped. image = imageModel. image
181
- #endif
182
- if let url = imageModel. url {
183
- view. wrapped. sd_setImage ( with: url, placeholderImage: placeholder, options: webOptions, context: webContext, progress: { ( receivedSize, expectedSize, _) in
184
- self . imageModel. progressBlock ? ( receivedSize, expectedSize)
185
- } ) { ( image, error, cacheType, _) in
186
- if let image = image {
187
- self . imageModel. successBlock ? ( image, cacheType)
188
- } else {
189
- self . imageModel. failureBlock ? ( error ?? NSError ( ) )
177
+ if let image = imageModel. image {
178
+ #if os(watchOS)
179
+ view. wrapped. setImage ( image)
180
+ #else
181
+ view. wrapped. image = image
182
+ #endif
183
+ } else {
184
+ if let url = url {
185
+ view. wrapped. sd_setImage ( with: url, placeholderImage: placeholder, options: webOptions, context: webContext, progress: { ( receivedSize, expectedSize, _) in
186
+ self . imageModel. progressBlock ? ( receivedSize, expectedSize)
187
+ } ) { ( image, error, cacheType, _) in
188
+ self . imageModel. image = image
189
+ if let image = image {
190
+ self . imageModel. successBlock ? ( image, cacheType)
191
+ } else {
192
+ self . imageModel. failureBlock ? ( error ?? NSError ( ) )
193
+ }
190
194
}
191
195
}
192
196
}
@@ -236,22 +240,22 @@ public struct AnimatedImage : PlatformViewRepresentable {
236
240
view. wrapped. imageScaling = . scaleProportionallyUpOrDown
237
241
#elseif os(iOS) || os(tvOS)
238
242
view. wrapped. contentMode = . scaleAspectFit
239
- #else
243
+ #elseif os(watchOS)
240
244
view. wrapped. setContentMode ( . aspectFit)
241
245
#endif
242
246
case . fill:
243
247
#if os(macOS)
244
248
view. wrapped. imageScaling = . scaleAxesIndependently
245
249
#elseif os(iOS) || os(tvOS)
246
250
view. wrapped. contentMode = . scaleToFill
247
- #else
251
+ #elseif os(watchOS)
248
252
view. wrapped. setContentMode ( . fill)
249
253
#endif
250
254
}
251
255
252
- #if os(macOS) || os(iOS) || os(tvOS)
253
256
// Animated Image does not support resizing mode and rendering mode
254
- if let image = view. wrapped. image, !image. sd_isAnimated, !image. conforms ( to: SDAnimatedImageProtocol . self) {
257
+ if let image = imageModel. image, !image. sd_isAnimated, !image. conforms ( to: SDAnimatedImageProtocol . self) {
258
+ var image = image
255
259
// ResizingMode
256
260
if let resizingMode = imageLayout. resizingMode {
257
261
#if os(macOS)
@@ -265,14 +269,24 @@ public struct AnimatedImage : PlatformViewRepresentable {
265
269
view. wrapped. image? . resizingMode = . stretch
266
270
view. wrapped. image? . capInsets = capInsets
267
271
#else
268
- view. wrapped. image = view. wrapped. image? . resizableImage ( withCapInsets: capInsets, resizingMode: . stretch)
272
+ image = image. resizableImage ( withCapInsets: capInsets, resizingMode: . stretch)
273
+ #if os(iOS) || os(tvOS)
274
+ view. wrapped. image = image
275
+ #elseif os(watchOS)
276
+ view. wrapped. setImage ( image)
277
+ #endif
269
278
#endif
270
279
case . tile:
271
280
#if os(macOS)
272
281
view. wrapped. image? . resizingMode = . tile
273
282
view. wrapped. image? . capInsets = capInsets
274
283
#else
275
- view. wrapped. image = view. wrapped. image? . resizableImage ( withCapInsets: capInsets, resizingMode: . tile)
284
+ image = image. resizableImage ( withCapInsets: capInsets, resizingMode: . tile)
285
+ #if os(iOS) || os(tvOS)
286
+ view. wrapped. image = image
287
+ #elseif os(watchOS)
288
+ view. wrapped. setImage ( image)
289
+ #endif
276
290
#endif
277
291
@unknown default :
278
292
// Future cases, not implements
@@ -287,13 +301,23 @@ public struct AnimatedImage : PlatformViewRepresentable {
287
301
#if os(macOS)
288
302
view. wrapped. image? . isTemplate = true
289
303
#else
290
- view. wrapped. image = view. wrapped. image? . withRenderingMode ( . alwaysTemplate)
304
+ image = image. withRenderingMode ( . alwaysTemplate)
305
+ #if os(iOS) || os(tvOS)
306
+ view. wrapped. image = image
307
+ #elseif os(watchOS)
308
+ view. wrapped. setImage ( image)
309
+ #endif
291
310
#endif
292
311
case . original:
293
312
#if os(macOS)
294
313
view. wrapped. image? . isTemplate = false
295
314
#else
296
- view. wrapped. image = view. wrapped. image? . withRenderingMode ( . alwaysOriginal)
315
+ image = image. withRenderingMode ( . alwaysOriginal)
316
+ #if os(iOS) || os(tvOS)
317
+ view. wrapped. image = image
318
+ #elseif os(watchOS)
319
+ view. wrapped. setImage ( image)
320
+ #endif
297
321
#endif
298
322
@unknown default :
299
323
// Future cases, not implements
@@ -302,6 +326,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
302
326
}
303
327
}
304
328
329
+ #if os(macOS) || os(iOS) || os(tvOS)
305
330
// Interpolation
306
331
if let interpolation = imageLayout. interpolation {
307
332
switch interpolation {
0 commit comments