SDWebImage / SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
https://sdwebimage.github.io/SDWebImageSwiftUI
MIT License
2.16k stars 223 forks source link

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

Open ykaito21 opened 3 years ago

ykaito21 commented 3 years ago

This doesn't work,

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

But this does work

AnimatedImage(url: URL(string: url))
   .resizable() 
   .scaledToFit()
Insofan commented 3 years ago

Sub code works.

AnimatedImage(url: URL(string: url))
                            .transition(.fade)
                           .resizable()
                           .scaledToFit()
ykaito21 commented 3 years ago

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

Insofan commented 3 years ago

@ykaito21 Declarative programming UI order is important

ykaito21 commented 3 years ago

@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()
dreampiggy commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

ykaito21 commented 3 years ago

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

dreampiggy commented 3 years ago

I'll have a try with that from stackoverflow:

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