What I want to achieve
I want the KFImage to always read from memory and disk cache, not redownloading the image every time. The user will have a smooth experience without seeing the image downloading for a few seconds everytime the view loads.
What is wrong
The KFImage view load its images from the profileImages
variable, which is initialized as empty list. That list does not get populated with URLs until getProfileImage()
called which takes a few seconds.
Even though the image URL is the same, there would be a few seconds of gap right after the view appears, so KFImage will not load from its cache, and need to redownload images. Is it possible to make the KFImage view always load from cache even if URL change? Or is there another solution to my problem?
import KingfisherSwiftUI
struct ProfileSubview: View {
@State var profileImages: [URL] = []
var body: some View {
ForEach(0..<profileImages.count, id: \.self){index in
KFImage(profileImages[index])
}
}
.onAppear{
getProfileImage()
}
}
func getProfileImage(){
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let storage = Storage.storage().reference().child("User_Profile")
storage.downloadURL{url, error in
if let error = error{
print("Error downloading image, \(error)")
}
profileImages.append(url!)
}
}