Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the assert then when using Data/Name in AnimatedImage #309

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading