Skip to content

Commit 8cf9a9d

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 e837c37 commit 8cf9a9d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,29 @@ public struct AnimatedImage : PlatformViewRepresentable {
301301
// Refresh image, imageModel is the Source of Truth, switch the type
302302
// Although we have Source of Truth, we can check the previous value, to avoid re-generate SDAnimatedImage, which is performance-cost.
303303
if let name = imageModel.name, name != context.coordinator.imageLoading.imageName {
304+
var image: PlatformImage?
304305
#if os(macOS)
305-
let image = SDAnimatedImage(named: name, in: imageModel.bundle)
306+
image = SDAnimatedImage(named: name, in: imageModel.bundle)
307+
if image == nil {
308+
// For static image, use NSImage as defaults
309+
let bundle = imageModel.bundle ?? .main
310+
image = bundle.image(forResource: name)
311+
}
306312
#else
307-
let image = SDAnimatedImage(named: name, in: imageModel.bundle, compatibleWith: nil)
313+
image = SDAnimatedImage(named: name, in: imageModel.bundle, compatibleWith: nil)
314+
if image == nil {
315+
// For static image, use UIImage as defaults
316+
image = PlatformImage(named: name, in: imageModel.bundle, compatibleWith: nil)
317+
}
308318
#endif
309319
context.coordinator.imageLoading.imageName = name
310320
view.wrapped.image = image
311321
} else if let data = imageModel.data, data != context.coordinator.imageLoading.imageData {
312-
let image = SDAnimatedImage(data: data, scale: imageModel.scale)
322+
var image: PlatformImage? = SDAnimatedImage(data: data, scale: imageModel.scale)
323+
if image == nil {
324+
// For static image, use UIImage as defaults
325+
image = PlatformImage(data: data)
326+
}
313327
context.coordinator.imageLoading.imageData = data
314328
view.wrapped.image = image
315329
} else if let url = imageModel.url {

0 commit comments

Comments
 (0)