Merge pull request #309 from SDWebImage/bugfix/animatedimage
Fix the assert then using Data/Name in AnimatedImage
This commit is contained in:
commit
c79754f7f6
|
@ -9,7 +9,9 @@
|
||||||
import UIKit
|
import UIKit
|
||||||
import SDWebImage
|
import SDWebImage
|
||||||
import SDWebImageWebPCoder
|
import SDWebImageWebPCoder
|
||||||
|
#if canImport(SDWebImageAVIFCoder)
|
||||||
import SDWebImageAVIFCoder
|
import SDWebImageAVIFCoder
|
||||||
|
#endif
|
||||||
import SDWebImageSVGCoder
|
import SDWebImageSVGCoder
|
||||||
import SDWebImagePDFCoder
|
import SDWebImagePDFCoder
|
||||||
|
|
||||||
|
@ -22,7 +24,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||||
// Override point for customization after application launch.
|
// Override point for customization after application launch.
|
||||||
// Add WebP/SVG/PDF support
|
// Add WebP/SVG/PDF support
|
||||||
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
|
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
|
||||||
|
#if canImport(SDWebImageAVIFCoder)
|
||||||
SDImageCodersManager.shared.addCoder(SDImageAVIFCoder.shared)
|
SDImageCodersManager.shared.addCoder(SDImageAVIFCoder.shared)
|
||||||
|
#endif
|
||||||
SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared)
|
SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared)
|
||||||
SDImageCodersManager.shared.addCoder(SDImagePDFCoder.shared)
|
SDImageCodersManager.shared.addCoder(SDImagePDFCoder.shared)
|
||||||
// Dynamic check to support vector format for both WebImage/AnimatedImage
|
// Dynamic check to support vector format for both WebImage/AnimatedImage
|
||||||
|
|
|
@ -34,6 +34,12 @@ struct ContentView3: View {
|
||||||
VStack {
|
VStack {
|
||||||
Text("\(animated ? "AnimatedImage" : "WebImage")")
|
Text("\(animated ? "AnimatedImage" : "WebImage")")
|
||||||
Spacer()
|
Spacer()
|
||||||
|
#if os(watchOS)
|
||||||
|
WebImage(url: url)
|
||||||
|
.resizable()
|
||||||
|
.scaledToFit()
|
||||||
|
.frame(width: 100, height: 100)
|
||||||
|
#else
|
||||||
if animated {
|
if animated {
|
||||||
AnimatedImage(url: url)
|
AnimatedImage(url: url)
|
||||||
.resizable()
|
.resizable()
|
||||||
|
@ -45,6 +51,7 @@ struct ContentView3: View {
|
||||||
.scaledToFit()
|
.scaledToFit()
|
||||||
.frame(width: 100, height: 100)
|
.frame(width: 100, height: 100)
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
Button("Toggle \(isOn ? "nil" : "valid") URL") {
|
Button("Toggle \(isOn ? "nil" : "valid") URL") {
|
||||||
isOn.toggle()
|
isOn.toggle()
|
||||||
}
|
}
|
||||||
|
|
|
@ -286,7 +286,10 @@ public struct AnimatedImage : PlatformViewRepresentable {
|
||||||
return view
|
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?
|
var image: PlatformImage?
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
image = SDAnimatedImage(named: name, in: imageModel.bundle)
|
image = SDAnimatedImage(named: name, in: imageModel.bundle)
|
||||||
|
@ -306,7 +309,10 @@ public struct AnimatedImage : PlatformViewRepresentable {
|
||||||
view.wrapped.image = image
|
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)
|
var image: PlatformImage? = SDAnimatedImage(data: data, scale: imageModel.scale)
|
||||||
if image == nil {
|
if image == nil {
|
||||||
// For static image, use UIImage as defaults
|
// 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
|
// 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.
|
// 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
|
let kind = imageModel.kind
|
||||||
if kind == .name, let name = imageModel.name, name != context.coordinator.imageLoading.imageName {
|
switch kind {
|
||||||
updateViewForName(name, view: view, context: context)
|
case .name:
|
||||||
} else if kind == .data, let data = imageModel.data, data != context.coordinator.imageLoading.imageData {
|
updateViewForName(imageModel.name, view: view, context: context)
|
||||||
updateViewForData(data, view: view, context: context)
|
case .data:
|
||||||
} else if kind == .url {
|
updateViewForData(imageModel.data, view: view, context: context)
|
||||||
|
case .url:
|
||||||
updateViewForURL(imageModel.url, view: view, context: context)
|
updateViewForURL(imageModel.url, view: view, context: context)
|
||||||
} else {
|
case .unknown:
|
||||||
fatalError("Unsupported model kind: \(kind)")
|
break // impossible
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
|
|
Loading…
Reference in New Issue