Skip to content

Commit 3a7132a

Browse files
committed
Try to fix the recusive updateView when using AnimatedImage inside ScrollView/LazyVStack. Because from iOS 14+, the @published update will always trigger another updateUIView
1 parent cf8d308 commit 3a7132a

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ final class AnimatedLoadingModel : ObservableObject, IndicatorReportable {
4343
@Published var image: PlatformImage? // loaded image, note when progressive loading, this will published multiple times with different partial image
4444
@Published var isLoading: Bool = false // whether network is loading or cache is querying, should only be used for indicator binding
4545
@Published var progress: Double = 0 // network progress, should only be used for indicator binding
46+
47+
/// Used for loading status recording to avoid recursive `updateView`. There are 3 types of loading (Name/Data/URL)
48+
@Published var imageName: String?
49+
@Published var imageData: Data?
50+
@Published var imageURL: URL?
4651
}
4752

4853
/// Completion Handler Binding Object, supports dynamic @State changes
@@ -228,15 +233,19 @@ public struct AnimatedImage : PlatformViewRepresentable {
228233
}
229234
self.imageHandler.progressBlock?(receivedSize, expectedSize)
230235
}) { (image, data, error, cacheType, finished, _) in
231-
// This is a hack because of Xcode 11.3 bug, the @Published does not trigger another `updateUIView` call
232-
// Here I have to use UIKit/AppKit API to triger the same effect (the window change implicitly cause re-render)
233-
if let hostingView = AnimatedImage.findHostingView(from: view) {
234-
if let _ = hostingView.window {
235-
#if os(macOS)
236-
hostingView.viewDidMoveToWindow()
237-
#else
238-
hostingView.didMoveToWindow()
239-
#endif
236+
if #available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) {
237+
// Do nothing
238+
} else {
239+
// This is a hack because of Xcode 11.3 bug, the @Published does not trigger another `updateUIView` call
240+
// Here I have to use UIKit/AppKit API to triger the same effect (the window change implicitly cause re-render)
241+
if let hostingView = AnimatedImage.findHostingView(from: view) {
242+
if let _ = hostingView.window {
243+
#if os(macOS)
244+
hostingView.viewDidMoveToWindow()
245+
#else
246+
hostingView.didMoveToWindow()
247+
#endif
248+
}
240249
}
241250
}
242251
self.imageLoading.image = image
@@ -263,19 +272,20 @@ public struct AnimatedImage : PlatformViewRepresentable {
263272
func updateView(_ view: AnimatedImageViewWrapper, context: Context) {
264273
// Refresh image, imageModel is the Source of Truth, switch the type
265274
// Although we have Source of Truth, we can check the previous value, to avoid re-generate SDAnimatedImage, which is performance-cost.
266-
if let name = imageModel.name, name != view.wrapped.sd_imageName {
275+
if let name = imageModel.name, name != imageLoading.imageName {
267276
#if os(macOS)
268277
let image = SDAnimatedImage(named: name, in: imageModel.bundle)
269278
#else
270279
let image = SDAnimatedImage(named: name, in: imageModel.bundle, compatibleWith: nil)
271280
#endif
272-
view.wrapped.sd_imageName = name
281+
imageLoading.imageName = name
273282
view.wrapped.image = image
274-
} else if let data = imageModel.data, data != view.wrapped.sd_imageData {
283+
} else if let data = imageModel.data, data != imageLoading.imageData {
275284
let image = SDAnimatedImage(data: data, scale: imageModel.scale)
276-
view.wrapped.sd_imageData = data
285+
imageLoading.imageData = data
277286
view.wrapped.image = image
278-
} else if let url = imageModel.url, url != view.wrapped.sd_imageURL {
287+
} else if let url = imageModel.url, url != imageLoading.imageURL {
288+
imageLoading.imageURL = url
279289
view.wrapped.sd_imageIndicator = imageConfiguration.indicator
280290
view.wrapped.sd_imageTransition = imageConfiguration.transition
281291
if let placeholderView = imageConfiguration.placeholderView {

SDWebImageSwiftUI/Classes/ImageViewWrapper.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,6 @@ public class AnimatedImageViewWrapper : PlatformView {
6666
}
6767
}
6868

69-
70-
/// Store the Animated Image loading state, to avoid re-query duinrg `updateView(_:)` until Source of Truth changes
71-
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
72-
extension PlatformView {
73-
static private var sd_imageNameKey: Void?
74-
static private var sd_imageDataKey: Void?
75-
76-
var sd_imageName: String? {
77-
get {
78-
objc_getAssociatedObject(self, &PlatformView.sd_imageNameKey) as? String
79-
}
80-
set {
81-
objc_setAssociatedObject(self, &PlatformView.sd_imageNameKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
82-
}
83-
}
84-
var sd_imageData: Data? {
85-
get {
86-
objc_getAssociatedObject(self, &PlatformView.sd_imageDataKey) as? Data
87-
}
88-
set {
89-
objc_setAssociatedObject(self, &PlatformView.sd_imageDataKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
90-
}
91-
}
92-
}
93-
9469
/// Use wrapper to solve the `UIProgressView`/`NSProgressIndicator` frame origin NaN crash (SwiftUI's bug)
9570
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
9671
public class ProgressIndicatorWrapper : PlatformView {

0 commit comments

Comments
 (0)