kean / Nuke

Image loading system
https://kean.blog/nuke
MIT License
8.07k stars 527 forks source link

Loading image at Tab View with FetchImage instance #486

Closed byunsangun closed 3 years ago

byunsangun commented 3 years ago

Hi I'm actually newbie of SwiftUI. I'm trying to implement TabView of image with url. If i use Nuke without TabView, it's working well. I think this error is related with my code because when i remove .onDisappear(perform: image.reset) this part, it's also working well... But i think it's not good solution. Can you check my code? Thank you in advance!!

Nuke Class ` struct RemoteImageView: View {

let url: URL

@StateObject private var image = FetchImage()

var body: some View {
    ZStack {
        Rectangle().fill(Color.gray)
        image.view?
            .resizable()
    }
    .onAppear() {
        image.load(url)
        print("on Appear called")
    }
    .onChange(of: url) {
        image.load($0)
        print("on changed called")
    }
    .onDisappear() {
        image.reset()
        print("on disappear called")
    }
}

}

`

Usage

TabView() {
    ForEach(0..<viewModel.photoUrls.count, id:\.self) { index in
        if let parsedUrl = URL(string: viewModel.photoUrls[index]) {
            RemoteImageView(url: parsedUrl)
                .frame(width: 400, height: 200) //FIXME: It should be changed into constant value
                .scaledToFit()
        } else {
             Rectangle()
                 .foregroundColor(.green)
                 .frame(width: 400, height: 200)
        }
     }
}
.tabViewStyle(PageTabViewStyle())
kean commented 3 years ago

I saw similar reports in other frameworks. It all depends on the view lifecycle of TabView subviews – I think it might be broken. I would suggest not calling reset() as a workaround.

You can add an option to RemoteImageView to configure the "on disappear" behavior the way I done in LazyImage.