@@ -16,11 +16,15 @@ import SwiftUI
1616@available ( iOS 14 . 0 , macOS 11 . 0 , tvOS 14 . 0 , watchOS 7 . 0 , * )
1717public class AnimatedImageViewWrapper : PlatformView {
1818 /// The wrapped actual image view, using SDWebImage's aniamted image view
19- public var wrapped = SDAnimatedImageView ( )
19+ @objc dynamic public var wrapped = SDAnimatedImageView ( )
20+ var observation : NSKeyValueObservation ?
2021 var interpolationQuality = CGInterpolationQuality . default
2122 var shouldAntialias = false
2223 var resizingMode : Image . ResizingMode ?
23- var imageSize : CGSize ?
24+
25+ deinit {
26+ observation? . invalidate ( )
27+ }
2428
2529 public override func draw( _ rect: CGRect ) {
2630 #if os(macOS)
@@ -50,15 +54,7 @@ public class AnimatedImageViewWrapper : PlatformView {
5054
5155 public override var intrinsicContentSize : CGSize {
5256 /// Match the behavior of SwiftUI.Image, only when image is resizable, use the super implementation to calculate size
53- var contentSize = wrapped. intrinsicContentSize
54- /// Sometimes, like during the transaction, the wrapped.image == nil, which cause contentSize invalid
55- /// Use image size as backup
56- /// TODO: This mixed use of UIKit/SwiftUI animation will cause visial issue because the intrinsicContentSize during animation may be changed
57- if let imageSize = imageSize {
58- if contentSize != imageSize {
59- contentSize = imageSize
60- }
61- }
57+ let contentSize = wrapped. intrinsicContentSize
6258 if let _ = resizingMode {
6359 /// Keep aspect ratio
6460 if contentSize. width > 0 && contentSize. height > 0 {
@@ -77,11 +73,17 @@ public class AnimatedImageViewWrapper : PlatformView {
7773 public override init ( frame frameRect: CGRect ) {
7874 super. init ( frame: frameRect)
7975 addSubview ( wrapped)
76+ observation = observe ( \. wrapped. image, options: [ . new] ) { _, _ in
77+ self . invalidateIntrinsicContentSize ( )
78+ }
8079 }
8180
8281 public required init ? ( coder: NSCoder ) {
8382 super. init ( coder: coder)
8483 addSubview ( wrapped)
84+ observation = observe ( \. wrapped. image, options: [ . new] ) { _, _ in
85+ self . invalidateIntrinsicContentSize ( )
86+ }
8587 }
8688}
8789
0 commit comments