Documentation/NukeUI.docc/Extensions/FetchImage-Extensions.md
NukeUI/FetchImageFetchImage is an observable object (ObservableObject) that allows you to manage the download of an image and observe the download status. It acts as a ViewModel that manages the image download state making it easy to add image loading to your custom SwiftUI views.
struct ImageView: View {
let url: URL
@StateObject private var image = FetchImage()
var body: some View {
ZStack {
Rectangle().fill(Color.gray)
image.image?
.resizable()
.aspectRatio(contentMode: .fill)
.clipped()
}
.onAppear { image.load(url) }
.onChange(of: url) { image.load($0) }
.onDisappear { image.reset() }
}
}
FetchImage gives you full control over how to manage the download and how to display the image. For example, if you want the download to continue when the view leaves the screen, change the appearance callbacks accordingly.
struct ImageView: View {
let url: URL
@StateObject private var image = FetchImage()
var body: some View {
// ...
.onAppear {
image.priority = .normal
image.load(url)
}
.onDisappear {
image.priority = .low
}
}
}
init()load(_:)-9my9qload(_:)-53ybwload(_:)-6pey2cancel()reset()resultimageContainerisLoadingprogress-swift.propertypriorityprocessorspipelinetransaction