Skip to content

When initializing a WebImage with isAnimating, if the initial value is false, the image never animates #332

Closed
@joshuatbrown

Description

@joshuatbrown

Given the following code:

struct MyView: View {
    let url: URL

    @State private var isAnimating = false

    var body: some View {
        ZStack {
            WebImage(url: url, isAnimating: $isAnimating)
            Button {
                 isAnimating = true
            } label: {
               Text(.localizable.gifButton)
            }
        }
    }
}

When I tap the button, the image will never start animating. It looks like the issue is in init(url:scale:options:context:isAnimating:transaction:content:) for WebImage here:

// provide animated image class if the initialized `isAnimating` is true, user can still custom the image class if they want
if isAnimating.wrappedValue {
    if context[.animatedImageClass] == nil {
        context[.animatedImageClass] = SDAnimatedImage.self
    }
}

So if the initial value of isAnimating is false, the context never gets an .animatedImageClass and therefore the image never animates.

This is unexpected -- the init accepts a Binding<Bool>, so if the value changes to true, I'd expect the image to start animating.

Here's my workaround, which I'd prefer not to do:

WebImage(url: url, context: [.animatedImageClass: SDAnimatedImage.self], isAnimating: $isAnimating)

When I do this, I can start animating the image by setting the value of my isAnimating to true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions