RTSDA-iOS/Services/ImageCache.swift
2025-02-03 16:15:57 -05:00

23 lines
606 B
Swift

import SwiftUI
actor ImageCache {
static let shared = ImageCache()
private let cache: NSCache<NSString, UIImage> = {
let cache = NSCache<NSString, UIImage>()
cache.countLimit = 100 // Maximum number of images to cache
return cache
}()
private init() {}
func image(for url: URL) -> UIImage? {
let key = url.absoluteString as NSString
return cache.object(forKey: key)
}
func setImage(_ image: UIImage, for url: URL) {
let key = url.absoluteString as NSString
cache.setObject(image, forKey: key)
}
}