I use the URLImage library, as it is lightweight and - as I thought - easy to use. I implemented a view to show text along with images that I load from the web. Like this:
struct MyView: View {
var body: some View {
ZStack {
Color.background.ignoresSafeArea()
ScrollView{
VStack(alignment: .leading) {
Text("Some text blabla")
AsyncDownloadImageView(imageUrl: myUrl)
}
.padding([.top, .leading, .trailing])
}
}
}
}
My AsyncDownloadImageView defines how the image view looks like during download, error, success, and whatever:
import SwiftUI
import URLImage
struct AsyncDownloadImageView: View {
let imageUrl: String
var body: some View {
URLImage (
URL( string: imageUrl )!
) {
// This view is displayed before download starts
EmptyView()
} inProgress: { progress in
// Display progress
HStack{
Spacer()
VStack(alignment: .center){
Image(systemName: "photo.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 60, height: 40.0)
.opacity(0.3)
Text("Waiting for server...")
.font(.footnote)
.fontWeight(.light)
.multilineTextAlignment(.center)
.opacity(0.3)
}
Spacer()
}
.frame(height: 100.0)
} failure: { error, retry in
// Display error and retry button
HStack{
Spacer()
VStack(alignment: .center){
Image(systemName: "wifi.slash")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 60, height: 40.0)
.opacity(0.3)
Text("Download failed. No network?")
.font(.footnote)
.fontWeight(.light)
.multilineTextAlignment(.center)
.opacity(0.3)
// Button("Retry", action: retry)
}
Spacer()
}
.frame(height: 100.0)
} content: { image in
// Downloaded image
image
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(10)
.shadow(radius: 5)
}
}
}
So far I am really happy with the results. But the pictures I am downloading are updated online every around 60 minutes. And I need to find a way to update the images I already displayed. How can I do that? My whole view seems to be persistent until I quit the app and start it again. Any hints?