Asynchronous Image
with downloader and caching, for SwiftUI
File > Add Package Dependencies > Enter Package URL: https://github.com/nbrandes/FastImage
Add a dependency in your Package.swift
.package(url: "https://github.com/nbrandes/FastImage.git"),
Initialize FastImage
with a URL
FastImage(url)
Provide a placeholder to be displayed while the image is loading.
If no closure is provided, a ProgressView
will be displayed.
FastImage(url) {
Text("Loading")
}
Required:
url: URL
- the URL
of an image to load
Optional:
aspectRatio: ContentMode
- (Default .fit) - constrains dimensions to the given size
progressWidth: CGFloat
- (Default 300) - width of the ProgressView
frame
progressHeight: Color
- (Default 200) - height of the ProgressView
frame
cache: Bool
- (Default true) - enable/disable image caching
placeholder: View
- ViewBuilder
that is displayed while the image is fetched
FastImage(url, progressWidth: 300, progressHeight: 200, cache: true) {
Text("Loading")
}
import SwiftUI
import FastImage
struct ContentView: View {
let paris = URL(string:"https://cdn.pixabay.com/photo/2019/02/21/18/52/paris-4011964_1280.jpg")!
var body: some View {
VStack() {
FastImage(paris)
}
}
}
For asynchronous downloading and caching without the SwiftUI component, use FastLoader
import FastImage
Task {
do {
let image = try await FastLoader().image(from: imageUrl)
// Do something with the image...
} catch {
print("Error: \(error.localizedDescription)")
}
}