SDWebImageSwiftUI icon indicating copy to clipboard operation
SDWebImageSwiftUI copied to clipboard

AnimatedImage with .transition() can't change its aspect ratio

Open ykaito21 opened this issue 4 years ago • 9 comments

This doesn't work,

AnimatedImage(url: URL(string: url))
   .resizable() 
   .transition(.fade)
   .scaledToFit()

But this does work

AnimatedImage(url: URL(string: url))
   .resizable() 
   .scaledToFit()

ykaito21 avatar Nov 13 '20 03:11 ykaito21

Sub code works.

AnimatedImage(url: URL(string: url))
                            .transition(.fade)
                           .resizable()
                           .scaledToFit()

Insofan avatar Nov 18 '20 01:11 Insofan

@Insofan Thanks! I think .scaledToFit() is also buggy. it preserved aspect ratio but doesn't scaled to its frame correctly.

ykaito21 avatar Nov 18 '20 02:11 ykaito21

@ykaito21 Declarative programming UI order is important

Insofan avatar Nov 18 '20 12:11 Insofan

@Insofan Thanks for your kind help. I understand UI order is important. I realized .scaledToFit() after .frame(width: 400, height: 200) does work but reverse case doesn't work. As documentation https://developer.apple.com/documentation/swiftui/view/scaledtofit(), I almost alway set .scaledToFit() before its frame without problem, and WebImage does work this way too. Is this AnimatedImage specific case or Am I missed something? But anyway thanks for your time and support.

This doesn't work:

AnimatedImage(url: URL(string: urlString))
    .resizable()
    .scaledToFit()
    .frame(width: 400, height: 200)

This does work:

AnimatedImage(url: URL(string: urlString))
    .resizable()
    .frame(width: 400, height: 200)
    .scaledToFit()

ykaito21 avatar Nov 20 '20 07:11 ykaito21

Known behavior, readme point to current AnimatedImage's limit because of SwiftUI's bug behavior for UIViewRepresentable:

image

https://github.com/SDWebImage/SDWebImageSwiftUI#using-animatedimage-to-play-animation

dreampiggy avatar Nov 20 '20 08:11 dreampiggy

UIViewRepresentable does not have anything context, to translate the View.aspectRatio, to UIView.contentMode. This suck makes me have to write a custom aspectRatio implementation (Which is not SwiftUI's implementation).

See source code: https://github.com/SDWebImage/SDWebImageSwiftUI/blob/1.5.0/SDWebImageSwiftUI/Classes/AnimatedImage.swift#L584


I haven't checked whether SwiftUI 2.0(iOS 14) have anything other solution to workaround this, if you can find some better idea, please help to create a MR ?

dreampiggy avatar Nov 20 '20 08:11 dreampiggy

Does these resource help for this task ?

https://stackoverflow.com/questions/57969470/trouble-to-make-a-custom-uiview-aspect-scale-fit-fill-with-swiftui https://stackoverflow.com/questions/60309187/fit-content-mode-for-a-custom-view-in-swiftui https://github.com/airbnb/lottie-ios/issues/1050

dreampiggy avatar Nov 20 '20 08:11 dreampiggy

@dreampiggy I got it! I really appreciate your detailed explanation and hard work on building an awesome package!

ykaito21 avatar Nov 20 '20 08:11 ykaito21

I'll have a try with that from stackoverflow:

imageView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
imageView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)

dreampiggy avatar Nov 20 '20 08:11 dreampiggy