Lazy loading of large number of images in a ZStack?

This question is about lazy fetching of images in, very specifically, SwiftUI:

Imagine a ZStack containing thousands of images, of varying sizes, laid out with no particular pattern (i.e. not a grid, nor horizontally splayed out, nor vertically. just haphazard) within a scrollview where you can zoom/pan in/out.

At any given time, you can probably see only a small smattering of the images.

What tools, if any, exist to allow lazy loading of the image data, to avoid even starting to get most of the images from somewhere, until they are on (or close to) being displayed?

Is there anything in the existing tool set of SwiftUI that will help or do I need to do this totally from scratch? (Not that I'm even sure how I'd do it lazily in UIKit.)

Any rough suggestions of what direction to start greatly appreciated. (The follow-on question is grabbing different versions of the image depending how big/small the displayed image is, to conserve memory).

1 Like

Here's one possible solution:

import SwiftUI

public struct LazyView<Content: View>: View {
    private let build: () -> Content
    public init(_ build: @autoclosure @escaping () -> Content) {
        self.build = build
    }
    public var body: Content {
        build()
    }
}

Source:

Terms of Service

Privacy Policy

Cookie Policy