SDWebImage / SDWebImageSwiftUI

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

WebImage doesn't update when the url changes #143

Closed ykaito21 closed 3 years ago

ykaito21 commented 3 years ago

I'm trying to update WebImage by changing its url, but the below code doesn't update WebImage even though VStack does update, am I wrong something? Sample Code:

 @State var urlString = "https://media0.giphy.com/media/3oEjHBop7WaPmuPryg/giphy.gif?cid=c3b5a7e2fdfc7510a373e125e129e199de61e11c3907222f&rid=giphy.gif"
    var body: some View {
        print("update view", urlString)
        return VStack {
            WebImage(url: URL(string: urlString), isAnimating: .constant(true))
                .resizable()
                .placeholder {
                    Rectangle().foregroundColor(.lightGray)
                }
                .indicator(.activity)
                .transition(.fade(duration: 0.5))
                .aspectRatio(contentMode: .fit)
                .frame(width: 200, height: 200)
            Button("test") {
                if urlString == "https://media0.giphy.com/media/3oEjHBop7WaPmuPryg/giphy.gif?cid=c3b5a7e2fdfc7510a373e125e129e199de61e11c3907222f&rid=giphy.gif" {
                    urlString = "https://media4.giphy.com/media/fLskTldNGlI3fISnLA/giphy.gif?cid=c3b5a7e2v2ma8dlz57l2swklkes7xi656k671vuhr5bxnkke&rid=giphy.gif"
                } else {
                    urlString = "https://media0.giphy.com/media/3oEjHBop7WaPmuPryg/giphy.gif?cid=c3b5a7e2fdfc7510a373e125e129e199de61e11c3907222f&rid=giphy.gif"
                }
            }
        }
loszhang commented 3 years ago

same error, GIF use AnimatedImage instead of WebImage

ykaito21 commented 3 years ago

@loszhang Thanks! AnimatedImage does work!

dreampiggy commented 3 years ago

😅 Seems this repo need more contributor to help for maintain. I'm busying in daily job and SDWebImage Core's related things.

I'll ask for some help for recently iOS 14 + SwiftUI behaviors and bugfix.

ykaito21 commented 3 years ago

@dreampiggy I don't have enough knowledge to fix the bug, but I really appreciate your hard work for this awesome package. @loszhang I'm using AnimatedImage with .resizable(), but it always stretch to fill its frame and .aspectRatio(contentMode: .fit) or .scaledToFit() doesn't seems to work. Do you know any way to solve this?

derikflanary commented 3 years ago

I figured out a way to update the WebImage. If you set a .id() as a modifier on the WebImage and set it to something like .id(url.absoluteString) then this will force the view to update when the URL changes

dreampiggy commented 3 years ago

This demo code seems work well on v2.0.0 version. So I just close ?