Skip to content

Commit 07fcee4

Browse files
committed
Allows to use UIImage/NSImage as defaults when init the AnimatedImage with JPEG data
This match the behavior when passing with the JPEG url initializer
1 parent 86b1901 commit 07fcee4

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,30 @@ public struct AnimatedImage : PlatformViewRepresentable {
286286
// Refresh image, imageModel is the Source of Truth, switch the type
287287
// Although we have Source of Truth, we can check the previous value, to avoid re-generate SDAnimatedImage, which is performance-cost.
288288
if let name = imageModel.name, name != context.coordinator.imageLoading.imageName {
289+
var image: PlatformImage?
289290
#if os(macOS)
290-
let image = SDAnimatedImage(named: name, in: imageModel.bundle)
291+
image = SDAnimatedImage(named: name, in: imageModel.bundle)
292+
if image == nil {
293+
// For static image, use NSImage as defaults
294+
let bundle = imageModel.bundle ?? .main
295+
image = bundle.image(forResource: name)
296+
}
291297
#else
292-
let image = SDAnimatedImage(named: name, in: imageModel.bundle, compatibleWith: nil)
298+
image = SDAnimatedImage(named: name, in: imageModel.bundle, compatibleWith: nil)
299+
if image == nil {
300+
// For static image, use UIImage as defaults
301+
image = PlatformImage(named: name, in: imageModel.bundle, compatibleWith: nil)
302+
}
293303
#endif
304+
294305
context.coordinator.imageLoading.imageName = name
295306
view.wrapped.image = image
296307
} else if let data = imageModel.data, data != context.coordinator.imageLoading.imageData {
297-
let image = SDAnimatedImage(data: data, scale: imageModel.scale)
308+
var image: PlatformImage? = SDAnimatedImage(data: data, scale: imageModel.scale)
309+
if image == nil {
310+
// For static image, use UIImage as defaults
311+
image = PlatformImage(data: data)
312+
}
298313
context.coordinator.imageLoading.imageData = data
299314
view.wrapped.image = image
300315
} else if let url = imageModel.url {

0 commit comments

Comments
 (0)