@@ -269,12 +269,11 @@ public struct AnimatedImage : PlatformViewRepresentable {
269
269
270
270
func layoutView( _ view: AnimatedImageViewWrapper , context: PlatformViewRepresentableContext < AnimatedImage > ) {
271
271
// AspectRatio
272
- if let _ = imageLayout. aspectRatio {
273
- // TODO: Needs layer transform and geometry calculation
274
- }
272
+ // If `aspectRatio` is `nil`, the resulting view maintains this view's aspect ratio.
273
+ let contentMode : ContentMode = imageLayout. aspectRatio == nil ? . fit : . fill
275
274
276
275
// ContentMode
277
- switch imageLayout . contentMode {
276
+ switch contentMode {
278
277
case . fit:
279
278
#if os(macOS)
280
279
view. wrapped. imageScaling = . scaleProportionallyUpOrDown
@@ -471,10 +470,18 @@ extension AnimatedImage {
471
470
/// fill the parent context.
472
471
/// - Returns: A view that constrains this view's dimensions to
473
472
/// `aspectRatio`, using `contentMode` as its scaling algorithm.
474
- public func aspectRatio( _ aspectRatio: CGFloat ? = nil , contentMode: ContentMode ) -> AnimatedImage {
473
+ public func aspectRatio( _ aspectRatio: CGFloat ? = nil , contentMode: ContentMode ) -> some View {
474
+ // The `SwifUI.View.aspectRatio(_:contentMode:)` says:
475
+ // If `aspectRatio` is `nil`, the resulting view maintains this view's aspect ratio
476
+ // But 1: there are no public API to declare what `this view's aspect ratio` is
477
+ // So, if we don't override this method, SwiftUI ignore the content mode on actual ImageView
478
+ // To workaround, we want to call the default `SwifUI.View.aspectRatio(_:contentMode:)` method
479
+ // But 2: there are no way to call a Protocol Extention default implementation in Swift 5.1
480
+ // So, we need a hack, that create a empty modifier, they call method on that view instead
481
+ // Fired Radar: FB7413534
475
482
imageLayout. aspectRatio = aspectRatio
476
483
imageLayout. contentMode = contentMode
477
- return self
484
+ return self . modifier ( EmptyModifier ( ) ) . aspectRatio ( aspectRatio , contentMode : contentMode )
478
485
}
479
486
480
487
/// Constrains this view's dimensions to the aspect ratio of the given size.
@@ -485,26 +492,28 @@ extension AnimatedImage {
485
492
/// fill the parent context.
486
493
/// - Returns: A view that constrains this view's dimensions to
487
494
/// `aspectRatio`, using `contentMode` as its scaling algorithm.
488
- public func aspectRatio( _ aspectRatio: CGSize , contentMode: ContentMode ) -> AnimatedImage {
495
+ public func aspectRatio( _ aspectRatio: CGSize , contentMode: ContentMode ) -> some View {
489
496
var ratio : CGFloat ?
490
497
if aspectRatio. width > 0 && aspectRatio. height > 0 {
491
498
ratio = aspectRatio. width / aspectRatio. height
499
+ } else {
500
+ NSException ( name: . invalidArgumentException, reason: " \( type ( of: self ) ) . \( #function) should be called with positive aspectRatio " , userInfo: nil ) . raise ( )
492
501
}
493
502
return self . aspectRatio ( ratio, contentMode: contentMode)
494
503
}
495
504
496
505
/// Scales this view to fit its parent.
497
506
/// - Returns: A view that scales this view to fit its parent,
498
507
/// maintaining this view's aspect ratio.
499
- public func scaledToFit( ) -> AnimatedImage {
500
- self . aspectRatio ( nil , contentMode: . fit)
508
+ public func scaledToFit( ) -> some View {
509
+ return self . aspectRatio ( nil , contentMode: . fit)
501
510
}
502
511
503
512
/// Scales this view to fill its parent.
504
513
/// - Returns: A view that scales this view to fit its parent,
505
514
/// maintaining this view's aspect ratio.
506
- public func scaledToFill( ) -> AnimatedImage {
507
- self . aspectRatio ( nil , contentMode: . fill)
515
+ public func scaledToFill( ) -> some View {
516
+ return self . aspectRatio ( nil , contentMode: . fill)
508
517
}
509
518
}
510
519
0 commit comments