0

I have an issue with loading Images from SwiftData or from Disk (Directory Folder) into a SwiftUI View. In the below example is the ScrollView with the LazyVGrid loading Images from SwiftData - (sortedPlaces) The issue is that when moving to that view (TabView) it takes approximately 400ms until the view displays and during scrolling it is noticeable that it lags.

Even if I save the pictures with low file size to disk (approx. 300KB per picture) and then loading from disk again, it is still very slow and lags during scrolling..

ScrollView(.vertical) {
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: Int(gridSize)), spacing: 5, pinnedViews: [.sectionHeaders]){
    ForEach(sortedPlaces) { picPlace in
        Image(uiImage: picPlace.image1)
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            .clipped()
            .aspectRatio(1, contentMode: .fit)
    }
}

}

But when change the code as below everything is fine but the display result, as you can imagine, is very bad..

    ScrollView(.vertical) {
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: Int(gridSize)), spacing: 5, pinnedViews: [.sectionHeaders]){
    ForEach(sortedPlaces) { picPlace in
        Image(uiImage: picPlace.image1)
        // without any modifiers
    }
}

}

It looks like the issue is with using "resizable()" and "frame(...)"

On the contrary, if I load a picture from the Asset Catalogue (with a size of approx. 8MB) 200 times (ForEach(0..<200) it is still fine, even with the modifiers...

Am I doing something wrong here or is this just what it is?

Can anyone give me some hints/ suggestions?

2
  • I am no expert but could it be that for each new image loaded all already loaded images gets resized again. Commented Oct 4, 2024 at 7:13
  • Interesting but I don’t think so. Loading an Image from the Assest Catalogue isn’t an issue, even when loading it 200 times. But I will run some tests on it. Thank you Commented Oct 4, 2024 at 9:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.