1 year ago

#36975

test-img

alionthego

How do I properly display very large images in a SwiftUI LazyVStack

I have a LazyVStack in a scrollView with many images. The size of the images in the scrollView is quite small, about 300x300 points. The images are extracted from Data stored in CoreData. The average image size is 1024x1024 and 500KB.

At the moment I am just loading the images without any caching or resizing using the something similar to the following:

var items: [Item]

var body: some View {
    GeometryReader { geometry in
        ScrollViewReader { scrollView in
            ScrollView(.vertical) {
                LazyVStack {
                    ForEach(items, id: \.id) { item in
                        Image(uiImage: UIImage(data: item.imageData!) ?? UIImage())
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 300, height: 300)
                    }
                }
            }
        }
    }
}

What I'm wondering is what is the best practice for this scenario. Am I needlessly slowing down my scroll by having the images resized? The original image sizes are 1024x1024 and about 500kb.

I have thought of creating another field in CoreData for thumbnail and resizing when I store there but that will use additional storage and I would like to avoid that if not necessary.

ios

xcode

swiftui

uiimage

lazyvstack

0 Answers

Your Answer

Accepted video resources