Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit f6ec37f

Browse files
authored
Add image caching
1 parent ff40092 commit f6ec37f

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import SwiftUI
2+
3+
final class ImageCache {
4+
static let shared = ImageCache()
5+
private var cache = NSCache<NSURL, UIImage>()
6+
7+
private init() {}
8+
9+
// Store UIImage
10+
func set(_ image: UIImage, for url: URL) {
11+
cache.setObject(image, forKey: url as NSURL)
12+
}
13+
14+
// Retrieve as SwiftUI Image
15+
func get(for url: URL) -> Image? {
16+
if let uiImage = cache.object(forKey: url as NSURL) {
17+
return Image(uiImage: uiImage)
18+
}
19+
return nil
20+
}
21+
}

Sources/prostore/views/appsView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ private struct AppRowView: View {
207207
.clipShape(RoundedRectangle(cornerRadius: 10))
208208
.shadow(radius: 1, y: 1)
209209
.onAppear {
210-
ImageCache.shared.set(image, for: iconURL)
210+
if let uiImage = image.asUIImage() {
211+
ImageCache.shared.set(uiImage, for: iconURL)
212+
}
211213
}
212214
case .failure:
213215
Image(systemName: "app")

0 commit comments

Comments
 (0)