Fix the assert then using Data/Name in AnimatedImage

Should match the logic as URL, allows to set
This commit is contained in:
DreamPiggy 2024-03-27 16:27:06 +08:00
parent 3333a1200c
commit b550096763
3 changed files with 27 additions and 9 deletions

View File

@ -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

View File

@ -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()
}

View File

@ -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)