-
Notifications
You must be signed in to change notification settings - Fork 5
/
Images.swift
40 lines (33 loc) · 1.37 KB
/
Images.swift
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
37
38
39
40
//
// Images.swift
//
// Created by Iiro Alhonen on 7.4.2022.
//
import SwiftUI
internal func formatImageComponents(_ string: String) -> (String, URL?) {
var altTextString = string
altTextString.removeSubrange(altTextString.startIndex...altTextString.index(altTextString.startIndex, offsetBy: 1))
let startIndex = altTextString.firstIndex(of: "]")
let endIndex = altTextString.index(before: altTextString.endIndex)
altTextString.removeSubrange((startIndex!)...endIndex)
var imageUrlString = string
imageUrlString.removeSubrange(imageUrlString.startIndex...imageUrlString.firstIndex(of: "(")!)
imageUrlString.removeSubrange(((imageUrlString.firstIndex(of: " ") ?? imageUrlString.firstIndex(of: ")")) ?? imageUrlString.index(before: imageUrlString.endIndex))...imageUrlString.index(before: imageUrlString.endIndex))
print(imageUrlString)
return (altTextString, URL(string: imageUrlString))
}
/**
Formats images.
- Parameter altText: String representing the alt text of the image.
- Parameter url: The url pointing to the image.
- Returns: a `some View` containing an `AsyncImage` view.
*/
@available(macOS 12.0, *)
internal func formatImage(altText: String?, url: URL?) -> some View {
let image = AsyncImage(url: url) { image in
image.accessibilityLabel(altText ?? "")
} placeholder: {
ProgressView()
}
return image
}