-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d79bec
commit 3d2535e
Showing
15 changed files
with
564 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Image.swift | ||
// ReadLess | ||
// | ||
// Created by thom on 10/4/23. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
public extension Image { | ||
|
||
static func loadAsync(from string: String) async -> Image? { | ||
guard let url = URL(string: string) else { return nil } | ||
return await self.loadAsync(from: url) | ||
} | ||
|
||
static func loadAsync(from url: URL?) async -> Image? { | ||
guard let url = url else { return nil } | ||
let request = URLRequest(url: url) | ||
guard let (data, _) = try? await URLSession.shared.data(for: request) else { return nil } | ||
guard let image = UIImage(data: data) else { return nil } | ||
return Image(uiImage: image) | ||
} | ||
|
||
static func load(from string: String, completion: @escaping @Sendable (_ image: Image?) -> Void) { | ||
guard let imageUrl = URL(string: string) else { return } | ||
return self.load(from: imageUrl, completion: completion) | ||
} | ||
|
||
static func load(from url: URL?, completion: @escaping @Sendable (_ image: Image?) -> Void) { | ||
guard let url = url else { | ||
completion(nil) | ||
return | ||
} | ||
URLSession.shared.dataTask(with: url) { data, _, error in | ||
if let data = data, let image = UIImage(data: data) { | ||
completion(Image(uiImage: image)) | ||
} | ||
}.resume() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.