Armin Scheithauer
Armin Scheithauer

Reputation: 598

Issues with displaying Images in SwiftUI

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?

Upvotes: 0

Views: 94

Answers (0)

Related Questions