AsyncImage doesn't work in images loaded for iOS widgets using SwiftUI

505 Views Asked by At

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.

0

There are 0 best solutions below