From b550096763c19a68a35819b6b3602767f6db073a Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 27 Mar 2024 16:27:06 +0800 Subject: [PATCH] Fix the assert then using Data/Name in AnimatedImage Should match the logic as URL, allows to set --- .../SDWebImageSwiftUIDemo/AppDelegate.swift | 4 +++ .../SDWebImageSwiftUIDemo/ContentView.swift | 7 ++++++ SDWebImageSwiftUI/Classes/AnimatedImage.swift | 25 ++++++++++++------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Example/SDWebImageSwiftUIDemo/AppDelegate.swift b/Example/SDWebImageSwiftUIDemo/AppDelegate.swift index 1b037d42..f08a8c8e 100644 --- a/Example/SDWebImageSwiftUIDemo/AppDelegate.swift +++ b/Example/SDWebImageSwiftUIDemo/AppDelegate.swift @@ -9,7 +9,9 @@ import UIKit import SDWebImage import SDWebImageWebPCoder +#if canImport(SDWebImageAVIFCoder) import SDWebImageAVIFCoder +#endif import SDWebImageSVGCoder import SDWebImagePDFCoder @@ -22,7 +24,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Override point for customization after application launch. // Add WebP/SVG/PDF support SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared) + #if canImport(SDWebImageAVIFCoder) SDImageCodersManager.shared.addCoder(SDImageAVIFCoder.shared) + #endif SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared) SDImageCodersManager.shared.addCoder(SDImagePDFCoder.shared) // Dynamic check to support vector format for both WebImage/AnimatedImage diff --git a/Example/SDWebImageSwiftUIDemo/ContentView.swift b/Example/SDWebImageSwiftUIDemo/ContentView.swift index e31d281a..6e7e74cf 100644 --- a/Example/SDWebImageSwiftUIDemo/ContentView.swift +++ b/Example/SDWebImageSwiftUIDemo/ContentView.swift @@ -34,6 +34,12 @@ struct ContentView3: View { VStack { Text("\(animated ? "AnimatedImage" : "WebImage")") Spacer() + #if os(watchOS) + WebImage(url: url) + .resizable() + .scaledToFit() + .frame(width: 100, height: 100) + #else if animated { AnimatedImage(url: url) .resizable() @@ -45,6 +51,7 @@ struct ContentView3: View { .scaledToFit() .frame(width: 100, height: 100) } + #endif Button("Toggle \(isOn ? "nil" : "valid") URL") { isOn.toggle() } diff --git a/SDWebImageSwiftUI/Classes/AnimatedImage.swift b/SDWebImageSwiftUI/Classes/AnimatedImage.swift index fd2fdf57..6c1b3ba4 100644 --- a/SDWebImageSwiftUI/Classes/AnimatedImage.swift +++ b/SDWebImageSwiftUI/Classes/AnimatedImage.swift @@ -286,7 +286,10 @@ public struct AnimatedImage : PlatformViewRepresentable { return view } - private func updateViewForName(_ name: String, view: AnimatedImageViewWrapper, context: Context) { + private func updateViewForName(_ name: String?, view: AnimatedImageViewWrapper, context: Context) { + guard let name = name, name != context.coordinator.imageLoading.imageName else { + return + } var image: PlatformImage? #if os(macOS) image = SDAnimatedImage(named: name, in: imageModel.bundle) @@ -306,7 +309,10 @@ public struct AnimatedImage : PlatformViewRepresentable { view.wrapped.image = image } - private func updateViewForData(_ data: Data, view: AnimatedImageViewWrapper, context: Context) { + private func updateViewForData(_ data: Data?, view: AnimatedImageViewWrapper, context: Context) { + guard let data = data, data != context.coordinator.imageLoading.imageData else { + return + } var image: PlatformImage? = SDAnimatedImage(data: data, scale: imageModel.scale) if image == nil { // For static image, use UIImage as defaults @@ -344,14 +350,15 @@ public struct AnimatedImage : PlatformViewRepresentable { // Refresh image, imageModel is the Source of Truth, switch the type // Although we have Source of Truth, we can check the previous value, to avoid re-generate SDAnimatedImage, which is performance-cost. let kind = imageModel.kind - if kind == .name, let name = imageModel.name, name != context.coordinator.imageLoading.imageName { - updateViewForName(name, view: view, context: context) - } else if kind == .data, let data = imageModel.data, data != context.coordinator.imageLoading.imageData { - updateViewForData(data, view: view, context: context) - } else if kind == .url { + switch kind { + case .name: + updateViewForName(imageModel.name, view: view, context: context) + case .data: + updateViewForData(imageModel.data, view: view, context: context) + case .url: updateViewForURL(imageModel.url, view: view, context: context) - } else { - fatalError("Unsupported model kind: \(kind)") + case .unknown: + break // impossible } #if os(macOS)