Home IOS Development ios – In SwiftUI I am downloading an Picture however it would not seem on display

ios – In SwiftUI I am downloading an Picture however it would not seem on display

0
ios – In SwiftUI I am downloading an Picture however it would not seem on display

[ad_1]

Small code the place I am downloading a picture efficiently however for some purpose the “self.picture = tmpImage” would not set off the view to be rebuild with the picture. Finished this a number of instances for some purpose I can not discover out it isn’t working for me.

last class ViewModel: ObservableObject {
    
    personal let imageManager: ImageManager
    @Revealed var picture: UIImage? = nil
    
    init(imageManager: ImageManager = ImageManager()) {
        self.imageManager = imageManager
    }
    
    func fetchRandomImage() async {
        let tmpImage = await imageManager.downloadAsyncAwait(URL(string: "https://github.githubassets.com/photos/icons/emoji/unicode/1f44d.png?v8")!)
        await MainActor.run {
            self.picture = tmpImage
        }
    }
}

struct ContentView: View {
    
    personal let viewModel = ViewModel()
    
    var physique: some View {
        VStack {
            if let picture = viewModel.picture {
                Picture(uiImage: picture)
                    .resizable()
                    .scaledToFit()
                    .body(width: 250, top: 250)
            }
        }
        .padding()
        .onAppear {
            Activity {
                await viewModel.fetchRandomImage()
            }
        }
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here