I have a list of products and each product will have their image. Whenever I open a list with more than 30 images, it will be a delay when opening the view, I assume this is happening because of SwiftUI loading the images from the cache.
This just happens if I use KFImage, whenever I use a AsyncImage, the view loads perfectly fine, in instant time, the problem just happens when I use KFImage.
By the way, when I use the KFImage, all the images are loaded right away when the view its presented, but the problem is that the view takes time to be presented..
this is my code:
List{
ScrollView {
VStack(spacing: 10) {
ForEach(categoryList, id: \.self) { myCategory in
VStack {
Button(action: {
if expandedList.contains(myCategory) {
expandedList.remove(myCategory)
} else {
expandedList.insert(myCategory)
}
}) {
HStack{
Text(myCategory).font(.system(size: 15)).fontWeight(.semibold).foregroundColor(Color(.systemGray))
}
}
if expandedList.contains(myCategory) {
ForEach(productsByCategory[myCategory]!, id: \.self.name) { product in
ZStack {
HStack (spacing: 10) {
KFImage(URL(string: product.imagine!) ?? URL(string: "https://myimage.com")!)
.resizable()
.cacheMemoryOnly(false)
.cacheOriginalImage(true)
.scaleFactor(UIScreen.main.scale)
.aspectRatio(contentMode: .fill)
.frame(width: 80, height: 80)
.clipShape(Circle())
.padding(.leading, 10)
How could I fix this delay?
In my opinion, it is because swiftui tries to load all the images from the cache/phone memory, but it takes time.
How can I change this? I want to use KFimage because I have a lot of options with this library.