SwiftUI's AsyncImage cache policy isn't adequate for our use, which means we're hitting the network too much. Let's write our own CachedAsyncImage view that will...
- Fetch images given a url
- Save images to the documents directory
- Load images from the documents directory
- Act as a drop in replacement for AsyncImage
For now we'll just implement Step 1.
Goals
- Dramatically decrease network traffic
- Local image storage for future features
Steps
Network
private func getImage(url: URL) async -> UIImage? {
do {
let request = URLRequest(url: url)
log("🌎 request: " + request.debugDescription)
let (data, response) = try await URLSession.shared.data(for: request)
log("🌎 response: " + response.debugDescription)
return UIImage(data: data)
}
catch {
log("🌎 error: " + error.localizedDescription)
return nil
}
}
func log(_ string: String) {
#if DEBUG
print(string)
#endif
}