How to force redraw element in swiftUI

270 Views Asked by At
VStack {

                WebImage(url: URL(string: (vm.user?.profilePictureUrl ?? "")))
                    .resizable()
                    .scaledToFill()
                    .frame(width: 128, height: 128)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color(.label), lineWidth: 0.5))
                    .shadow(radius: 0.5)
                    .padding()
                    

            PhotosPicker(selection: $selectedItem) {
                Label("Change Profile Photo", systemImage: "camera")
            }
            .onChange(of: selectedItem) { newValue in
                Task {
                    if let imageData = try? await newValue?
                        .loadTransferable(type: Data.self), let image = UIImage(data: imageData) {
                        selectedImage = Image(uiImage: image)
                        persistImageToStorage(image: image)
                    }
                }
            }
            .padding()
            .foregroundColor(.accentColor)
            .background(.ultraThickMaterial)
            .cornerRadius(10)
            .frame(minWidth: 0, maxWidth: .infinity)

I have WebImage with a link that does not update Only the content of the link updates when I perform persistImageToStorage function Thus, when I selcet a new photo to be presented, it redraws only if I force redraw it in Xcode. How can I tell my program to redraw WebImage element when I selcet a new image?

AsyncImage(url: URL(string: vm.user?.profilePictureUrl ?? "")) { phase in
                switch phase {
                case.success(let image):
                    image.resizable()
                case .empty:
                    ProgressView()
                case .failure(_):
                    EmptyView()
                @unknown default:
                    EmptyView()
                }
            }
                    .frame(width: 128, height: 128)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color(.label), lineWidth: 0.5))
                    .shadow(radius: 0.5)
                    .padding()
0

There are 0 best solutions below