-
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.
Merge pull request #687 from NoodleOfDeath/dev
~sitemaps
- Loading branch information
Showing
42 changed files
with
1,770 additions
and
319 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
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,51 @@ | ||
// | ||
// Image.swift | ||
// ReadLess | ||
// | ||
// Created by thom on 10/4/23. | ||
// | ||
|
||
import Foundation | ||
import CoreGraphics | ||
import SwiftUI | ||
|
||
public extension Image { | ||
|
||
static func loadAsync(from string: String, maxWidth: CGFloat? = nil) async -> Image? { | ||
guard let url = URL(string: string) else { return nil } | ||
return await self.loadAsync(from: url, maxWidth: maxWidth) | ||
} | ||
|
||
static func loadAsync(from url: URL?, maxWidth: CGFloat? = nil) 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 } | ||
if let maxWidth = maxWidth, let image = image.resized(toWidth: maxWidth) { | ||
return Image(uiImage: image) | ||
} | ||
return Image(uiImage: image) | ||
} | ||
|
||
static func load(from string: String, maxWidth: CGFloat? = nil, 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?, maxWidth: CGFloat? = nil, 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) { | ||
if let maxWidth = maxWidth, let image = image.resized(toWidth: maxWidth) { | ||
completion(Image(uiImage: image)) | ||
} else { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// UIImage.swift | ||
// ReadLess | ||
// | ||
// Created by thom on 10/6/23. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
extension UIImage { | ||
|
||
func resized(toWidth width: CGFloat, isOpaque: Bool = true) -> UIImage? { | ||
let size = CGSize(width: width, height: width / size.width * size.height) | ||
return self.resized(toSize: size, isOpaque: isOpaque) | ||
} | ||
|
||
func resized(toSize size: CGSize, isOpaque: Bool = true) -> UIImage? { | ||
UIGraphicsBeginImageContextWithOptions(size, true, 1.0) | ||
self.draw(in: CGRect(origin: .zero, size: size)) | ||
defer { UIGraphicsEndImageContext() } | ||
return UIGraphicsGetImageFromCurrentImageContext() | ||
} | ||
|
||
} |
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.