Skip to content

Commit 26f7571

Browse files
committed
Fix the WebImage.transaction should use take effect
1 parent 02b2579 commit 26f7571

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

SDWebImageSwiftUI/Classes/ImageManager.swift

+15-12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public final class ImageManager : ObservableObject {
6060
weak var currentOperation: SDWebImageOperation? = nil
6161

6262
var currentURL: URL?
63+
var transaction = Transaction()
6364
var successBlock: ((PlatformImage, Data?, SDImageCacheType) -> Void)?
6465
var failureBlock: ((Error) -> Void)?
6566
var progressBlock: ((Int, Int) -> Void)?
@@ -106,18 +107,20 @@ public final class ImageManager : ObservableObject {
106107
// So previous View struct call `onDisappear` and cancel the currentOperation
107108
return
108109
}
109-
self.image = image
110-
self.error = error
111-
self.isIncremental = !finished
112-
if finished {
113-
self.imageData = data
114-
self.cacheType = cacheType
115-
self.indicatorStatus.isLoading = false
116-
self.indicatorStatus.progress = 1
117-
if let image = image {
118-
self.successBlock?(image, data, cacheType)
119-
} else {
120-
self.failureBlock?(error ?? NSError())
110+
withTransaction(transaction) {
111+
self.image = image
112+
self.error = error
113+
self.isIncremental = !finished
114+
if finished {
115+
self.imageData = data
116+
self.cacheType = cacheType
117+
self.indicatorStatus.isLoading = false
118+
self.indicatorStatus.progress = 1
119+
if let image = image {
120+
self.successBlock?(image, data, cacheType)
121+
} else {
122+
self.failureBlock?(error ?? NSError())
123+
}
121124
}
122125
}
123126
}

SDWebImageSwiftUI/Classes/WebImage.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ final class WebImageConfiguration: ObservableObject {
8181
/// A Image View type to load image from url. Supports static/animated image format.
8282
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
8383
public struct WebImage<Content> : View where Content: View {
84-
var transaction: Transaction
85-
8684
var configurations: [(Image) -> Image] = []
8785

8886
var content: (WebImagePhase) -> Content
@@ -146,10 +144,10 @@ public struct WebImage<Content> : View where Content: View {
146144
imageModel.context = context
147145
_imageModel = ObservedObject(wrappedValue: imageModel)
148146
let imageManager = ImageManager()
147+
imageManager.transaction = transaction
149148
_imageManager = StateObject(wrappedValue: imageManager)
150149
_indicatorStatus = ObservedObject(wrappedValue: imageManager.indicatorStatus)
151150

152-
self.transaction = transaction
153151
self.content = { phase in
154152
content(phase)
155153
}

0 commit comments

Comments
 (0)