Skip to content

Commit b7e5780

Browse files
committed
workaround ratio
1 parent 74948b8 commit b7e5780

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,11 @@ extension AnimatedImage {
622622
// Aspect Ratio
623623
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
624624
extension AnimatedImage {
625+
func setImageLayoutAspectRatio(_ aspectRatio: CGFloat?, contentMode: ContentMode) {
626+
self.imageLayout.aspectRatio = aspectRatio
627+
self.imageLayout.contentMode = contentMode
628+
}
629+
625630
/// Constrains this view's dimensions to the specified aspect ratio.
626631
/// - Parameters:
627632
/// - aspectRatio: The ratio of width to height to use for the resulting
@@ -631,6 +636,7 @@ extension AnimatedImage {
631636
/// fill the parent context.
632637
/// - Returns: A view that constrains this view's dimensions to
633638
/// `aspectRatio`, using `contentMode` as its scaling algorithm.
639+
@ViewBuilder
634640
public func aspectRatio(_ aspectRatio: CGFloat? = nil, contentMode: ContentMode) -> some View {
635641
// The `SwifUI.View.aspectRatio(_:contentMode:)` says:
636642
// If `aspectRatio` is `nil`, the resulting view maintains this view's aspect ratio
@@ -640,9 +646,12 @@ extension AnimatedImage {
640646
// But 2: there are no way to call a Protocol Extention default implementation in Swift 5.1
641647
// So, we directly call the implementation detail modifier instead
642648
// Fired Radar: FB7413534
643-
self.imageLayout.aspectRatio = aspectRatio
644-
self.imageLayout.contentMode = contentMode
645-
return self.modifier(_AspectRatioLayout(aspectRatio: aspectRatio, contentMode: contentMode))
649+
let _ = self.setImageLayoutAspectRatio(aspectRatio, contentMode: contentMode)
650+
if let aspectRatio {
651+
self.modifier(_AspectRatioLayout(aspectRatio: aspectRatio, contentMode: contentMode))
652+
} else {
653+
self
654+
}
646655
}
647656

648657
/// Constrains this view's dimensions to the aspect ratio of the given size.

0 commit comments

Comments
 (0)