Skip to content

Commit

Permalink
Fix the assert then using Data/Name in AnimatedImage
Browse files Browse the repository at this point in the history
Should match the logic as URL, allows to set
  • Loading branch information
dreampiggy committed Mar 27, 2024
1 parent 3333a12 commit b550096
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Example/SDWebImageSwiftUIDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import UIKit
import SDWebImage
import SDWebImageWebPCoder
#if canImport(SDWebImageAVIFCoder)
import SDWebImageAVIFCoder
#endif
import SDWebImageSVGCoder
import SDWebImagePDFCoder

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Example/SDWebImageSwiftUIDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -45,6 +51,7 @@ struct ContentView3: View {
.scaledToFit()
.frame(width: 100, height: 100)
}
#endif
Button("Toggle \(isOn ? "nil" : "valid") URL") {
isOn.toggle()
}
Expand Down
25 changes: 16 additions & 9 deletions SDWebImageSwiftUI/Classes/AnimatedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b550096

Please sign in to comment.