Skip to content

Commit 5d6502a

Browse files
arnauddorgansdreampiggy
authored andcommitted
workaround ratio
(cherry picked from commit b7e5780)
1 parent cf0dc79 commit 5d6502a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Diff for: SDWebImageSwiftUI/Classes/AnimatedImage.swift

+12-3
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,11 @@ extension AnimatedImage {
600600
// Aspect Ratio
601601
@available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *)
602602
extension AnimatedImage {
603+
func setImageLayoutAspectRatio(_ aspectRatio: CGFloat?, contentMode: ContentMode) {
604+
self.imageLayout.aspectRatio = aspectRatio
605+
self.imageLayout.contentMode = contentMode
606+
}
607+
603608
/// Constrains this view's dimensions to the specified aspect ratio.
604609
/// - Parameters:
605610
/// - aspectRatio: The ratio of width to height to use for the resulting
@@ -609,6 +614,7 @@ extension AnimatedImage {
609614
/// fill the parent context.
610615
/// - Returns: A view that constrains this view's dimensions to
611616
/// `aspectRatio`, using `contentMode` as its scaling algorithm.
617+
@ViewBuilder
612618
public func aspectRatio(_ aspectRatio: CGFloat? = nil, contentMode: ContentMode) -> some View {
613619
// The `SwifUI.View.aspectRatio(_:contentMode:)` says:
614620
// If `aspectRatio` is `nil`, the resulting view maintains this view's aspect ratio
@@ -618,9 +624,12 @@ extension AnimatedImage {
618624
// But 2: there are no way to call a Protocol Extention default implementation in Swift 5.1
619625
// So, we directly call the implementation detail modifier instead
620626
// Fired Radar: FB7413534
621-
self.imageLayout.aspectRatio = aspectRatio
622-
self.imageLayout.contentMode = contentMode
623-
return self.modifier(_AspectRatioLayout(aspectRatio: aspectRatio, contentMode: contentMode))
627+
let _ = self.setImageLayoutAspectRatio(aspectRatio, contentMode: contentMode)
628+
if let aspectRatio {
629+
self.modifier(_AspectRatioLayout(aspectRatio: aspectRatio, contentMode: contentMode))
630+
} else {
631+
self
632+
}
624633
}
625634

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

0 commit comments

Comments
 (0)