I am storing images in Core Data as binary data, and showing them in another view via a LazyVGrid to replicate a CollectionView in UIKit. However, performance seems to be really choppy/poor on scroll and was wondering if there's any improvements that can be made.
I think it doesn't like that I am creating images in the view, but I don't see a reusableCell-type component in SwiftUI.
Here's my existing code:
import Foundation
import SwiftUI
struct LibraryView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(entity: Images.entity(), sortDescriptors: [], predicate: nil)
private var images: FetchedResults<Images>
private var threeColumnGrid = [GridItem(.flexible(minimum: 80)), GridItem(.flexible(minimum: 80)), GridItem(.flexible(minimum: 80))]
var body: some View {
ScrollView {
LazyVGrid(columns: threeColumnGrid) {
ForEach(self.images, id: \.self) { fetchedImg in
GeometryReader { gr in
if let data = fetchedImg.image,
let image = UIImage(data: data) {
Image(uiImage: image)
.resizable()
.scaledToFill()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.aspectRatio(1, contentMode: .fill)
.background(Color.gray)
}
}
.clipped()
.cornerRadius(10)
.aspectRatio(1, contentMode: .fit)
}
}
.padding(5)
}
}
}
Any tips/suggestions would be greatly appreciated. Thanks!