Skip to content

Commit 911f871

Browse files
committed
Change the image using @State instead of ObservedObject
1 parent 09f2f74 commit 911f871

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import SDWebImage
1212
import SDWebImageSwiftUIObjC
1313
#endif
1414

15-
// Data Binding Object
16-
final class AnimatedImageModel : ObservableObject {
17-
@Published var image: PlatformImage?
18-
}
19-
2015
// Convenient
2116
#if os(watchOS)
2217
public typealias AnimatedImageViewWrapper = SDAnimatedImageInterface
@@ -39,10 +34,9 @@ public final class AnimatedImageCoordinator: NSObject {
3934

4035
// View
4136
public struct AnimatedImage : PlatformViewRepresentable {
42-
@ObservedObject var imageModel = AnimatedImageModel()
43-
4437
// Options
4538
var url: URL?
39+
@State var image: PlatformImage?
4640
var webOptions: SDWebImageOptions = []
4741
var webContext: [SDWebImageContextOption : Any]? = nil
4842

@@ -84,11 +78,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
8478
/// True to start animation, false to stop animation.
8579
@Binding public var isAnimating: Bool
8680

87-
/// Current loaded image, may be `SDAnimatedImage` type
88-
public var image: PlatformImage? {
89-
imageModel.image
90-
}
91-
9281
/// Create an animated image with url, placeholder, custom options and context.
9382
/// - Parameter url: The image url
9483
/// - Parameter placeholder: The placeholder image to show during loading
@@ -131,7 +120,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
131120
#else
132121
let image = SDAnimatedImage(named: name, in: bundle, compatibleWith: nil)
133122
#endif
134-
self.imageModel.image = image
123+
self.image = image
135124
}
136125

137126
/// Create an animated image with data and scale.
@@ -148,7 +137,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
148137
public init(data: Data, scale: CGFloat = 0, isAnimating: Binding<Bool>) {
149138
self._isAnimating = isAnimating
150139
let image = SDAnimatedImage(data: data, scale: scale)
151-
self.imageModel.image = image
140+
self.image = image
152141
}
153142

154143
#if os(macOS)
@@ -212,8 +201,10 @@ public struct AnimatedImage : PlatformViewRepresentable {
212201
view.wrapped.sd_setImage(with: url, placeholderImage: placeholder, options: webOptions, context: webContext, progress: { (receivedSize, expectedSize, _) in
213202
self.progressBlock?(receivedSize, expectedSize)
214203
}) { (image, error, cacheType, _) in
204+
DispatchQueue.main.async {
205+
self.image = image
206+
}
215207
if let image = image {
216-
self.imageModel.image = image
217208
self.successBlock?(image, cacheType)
218209
} else {
219210
self.failureBlock?(error ?? NSError())
@@ -235,7 +226,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
235226
view.wrapped.animates = true
236227
#endif
237228

238-
if let image = self.imageModel.image {
229+
if let image = self.image {
239230
#if os(watchOS)
240231
view.wrapped.setImage(image)
241232
#else
@@ -349,7 +340,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
349340
#endif
350341

351342
// Animated Image does not support resizing mode and rendering mode
352-
if let image = self.imageModel.image, !image.sd_isAnimated, !image.conforms(to: SDAnimatedImageProtocol.self) {
343+
if let image = self.image, !image.sd_isAnimated, !image.conforms(to: SDAnimatedImageProtocol.self) {
353344
var image = image
354345
// ResizingMode
355346
if let resizingMode = self.resizingMode {

0 commit comments

Comments
 (0)