Skip to content

Commit 24be9b2

Browse files
committed
feat: 로컬 파일 URL도 가능하도록 구현
1 parent 6c22eca commit 24be9b2

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

DevLog/UI/Common/Componeent/CacheableImage.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ struct CacheableImage<Content: View>: View {
5757

5858
@MainActor
5959
private func loadImageWithCache() async {
60-
guard self.url != nil else { return }
60+
guard let url = self.url else { return }
61+
62+
if url.isFileURL {
63+
await loadLocalImage(from: url)
64+
return
65+
}
6166

6267
if let cachedResponse = URLCache.imageCached.cachedResponse(for: request) {
6368
if let uiImage = UIImage(data: cachedResponse.data) {
@@ -81,6 +86,23 @@ struct CacheableImage<Content: View>: View {
8186
isInvalid = true
8287
}
8388
}
89+
90+
@MainActor
91+
private func loadLocalImage(from url: URL) async {
92+
do {
93+
let data = try await Task.detached {
94+
try Data(contentsOf: url)
95+
}.value
96+
97+
if let uiImage = UIImage(data: data) {
98+
self.loadedUIImage = uiImage
99+
} else {
100+
isInvalid = true
101+
}
102+
} catch {
103+
isInvalid = true
104+
}
105+
}
84106
}
85107

86108
extension URLCache {

0 commit comments

Comments
 (0)