Skip to content

Fix AnimatedImage aspectRatio issue when ratio is nil #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions SDWebImageSwiftUI/Classes/AnimatedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,11 @@ extension AnimatedImage {
// Aspect Ratio
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension AnimatedImage {
func setImageLayoutAspectRatio(_ aspectRatio: CGFloat?, contentMode: ContentMode) {
self.imageLayout.aspectRatio = aspectRatio
self.imageLayout.contentMode = contentMode
}

/// Constrains this view's dimensions to the specified aspect ratio.
/// - Parameters:
/// - aspectRatio: The ratio of width to height to use for the resulting
Expand All @@ -631,6 +636,7 @@ extension AnimatedImage {
/// fill the parent context.
/// - Returns: A view that constrains this view's dimensions to
/// `aspectRatio`, using `contentMode` as its scaling algorithm.
@ViewBuilder
Copy link
Collaborator

@dreampiggy dreampiggy Mar 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need ? Seems I can not get what cause this.


Oh, I understand. You change this function to a ViewBuilder with if-else condition and return different View type for (some View), so this is needed

public func aspectRatio(_ aspectRatio: CGFloat? = nil, contentMode: ContentMode) -> some View {
// The `SwifUI.View.aspectRatio(_:contentMode:)` says:
// If `aspectRatio` is `nil`, the resulting view maintains this view's aspect ratio
Expand All @@ -640,9 +646,12 @@ extension AnimatedImage {
// But 2: there are no way to call a Protocol Extention default implementation in Swift 5.1
// So, we directly call the implementation detail modifier instead
// Fired Radar: FB7413534
self.imageLayout.aspectRatio = aspectRatio
self.imageLayout.contentMode = contentMode
return self.modifier(_AspectRatioLayout(aspectRatio: aspectRatio, contentMode: contentMode))
let _ = self.setImageLayoutAspectRatio(aspectRatio, contentMode: contentMode)
if let aspectRatio {
self.modifier(_AspectRatioLayout(aspectRatio: aspectRatio, contentMode: contentMode))
} else {
self
}
}

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