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:
and this one for second:
Why doesn't it work correctly on iOS widgets? The same code loads image very good on watchOS.