SDWebImageSwiftUI
SDWebImageSwiftUI copied to clipboard
AnimatedImage with .transition() can't change its aspect ratio
This doesn't work,
AnimatedImage(url: URL(string: url))
.resizable()
.transition(.fade)
.scaledToFit()
But this does work
AnimatedImage(url: URL(string: url))
.resizable()
.scaledToFit()
Sub code works.
AnimatedImage(url: URL(string: url))
.transition(.fade)
.resizable()
.scaledToFit()
@Insofan Thanks! I think .scaledToFit()
is also buggy. it preserved aspect ratio but doesn't scaled to its frame correctly.
@ykaito21 Declarative programming UI order is important
@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()
Known behavior, readme point to current AnimatedImage's limit because of SwiftUI's bug behavior for UIViewRepresentable
:
https://github.com/SDWebImage/SDWebImageSwiftUI#using-animatedimage-to-play-animation
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 ?
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 I got it! I really appreciate your detailed explanation and hard work on building an awesome package!
I'll have a try with that from stackoverflow:
imageView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
imageView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)