2

This is my simple class file:

import SwiftUI
struct NetworkImage: View {
    let url: URL?
    var body: some View {
//1st part
        if let url = URL(string: "https://thumbs.dreamstime.com/b/tiger-portrait-horizontal-11392212.jpg"),
            let imageData = try? Data(contentsOf: url),
            let uiImage = UIImage(data: imageData) {
            Image(uiImage: uiImage)
                .centerCropped()
        }
// 2nd part
            AsyncImage(url: URL(string: "https://thumbs.dreamstime.com/b/tiger-portrait-horizontal-11392212.jpg")) { phase in
                if let image = phase.image {
                    image
                        .centerCropped()
                } else {
                    Image(systemName: "photo.fill")
                        .centerCropped()
                        .opacity(0.3)
                }
            }
}
extension Image {
    func centerCropped() -> some View {
        GeometryReader { geo in
            self
            .resizable()
            .scaledToFill()
                .frame(width: geo.size.width, height: geo.size.height)
            .clipped()
        }
    }
}

This is the result for first part:

enter image description here

and this one for second:

enter image description here

Why doesn't it work correctly on iOS widgets? The same code loads image very good on watchOS.

12
  • 1
    Widgets don’t listen or update my guess would be that AsyncImage relies on SwiftUI wrappers to listen when the download is complete. Commented Mar 18, 2023 at 10:27
  • 1
    What do you mean?;) Because I dont understand. SwiftUI wrappers?;) Commented Mar 18, 2023 at 11:31
  • 1
    SwiftUI depends on wrappers that conform to DynamicProperty to tell it when to reload the body. Widgets can’t reload the body. No SwiftUI wrappers work like they do in iOS or watchOS. AsyncImage likely has a wrapper within it to reload its body when the download is done. Since wrappers don’t work async image doesn’t work. Commented Mar 18, 2023 at 11:42
  • 1
    Widgets are like a GIF and each Entry is like an image that makes the GIF. If the data doesn’t exist in get timeline when you are making the gif/entries then it won’t exist at any other point. Commented Mar 18, 2023 at 11:44
  • 2
    Nothing else right now. Widgets are meant to be low weight. But it may change in June there has been quite a bit of talk about animations for widgets which isn’t possible for the same reasons. This is just a hunch though nothing concrete. Commented Mar 18, 2023 at 12:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.