Skip to content

Commit bfc1ea7

Browse files
Add macOS support
1 parent 6957e79 commit bfc1ea7

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Sources/CachedAsyncImage/CachedAsyncImage.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ public struct CachedAsyncImage<Content>: View where Content: View {
105105
/// would name with the `@2x` suffix if stored in a file on disk.
106106
public init(url: URL?, urlCache: URLCache = .shared, scale: CGFloat = 1) where Content == Image {
107107
self.init(url: url, urlCache: urlCache, scale: scale) { phase in
108+
#if os(macOS)
109+
phase.image ?? Image(nsImage: .init())
110+
#else
108111
phase.image ?? Image(uiImage: .init())
112+
#endif
109113
}
110114
}
111115

@@ -201,10 +205,17 @@ public struct CachedAsyncImage<Content>: View where Content: View {
201205
guard let url = url else { return }
202206
let request = URLRequest(url: url)
203207
let (data, _) = try await urlSession.data(for: request)
208+
#if os(macOS)
209+
if let nsImage = NSImage(data: data) {
210+
let image = Image(nsImage: nsImage)
211+
phase = .success(image)
212+
}
213+
#else
204214
if let uiImage = UIImage(data: data) {
205215
let image = Image(uiImage: uiImage)
206216
phase = .success(image)
207217
}
218+
#endif
208219
} catch {
209220
phase = .failure(error)
210221
}

0 commit comments

Comments
 (0)