blob: cc5cb7ebca428e18e539542e5844e89c538dcd6f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import Combine
import SwiftUI
class ImageCacheManager {
static let shared = ImageCacheManager()
private let cache: URLCache
private var cancellables: Set<AnyCancellable> = []
init() {
cache = URLCache(
memoryCapacity: 100 * 1_024 * 1_024, // 100 MB
diskCapacity: 500 * 1_024 * 1_024, // 500 MB
diskPath: "SoraImageCache"
)
URLCache.shared = cache
}
func preloadImages(_ urls: [URL]) {
urls.forEach { url in
URLSession.shared.dataTaskPublisher(for: url)
.sink(
receiveCompletion: { _ in () },
receiveValue: { data, response in
let cachedResponse = CachedURLResponse(
response: response,
data: data
)
self.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url))
}
)
.store(in: &cancellables)
}
}
}
|