-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathImageRequest.swift
84 lines (69 loc) · 2.38 KB
/
ImageRequest.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import UIKit
public final class ImageRequest: Sendable {
public enum Source: Sendable {
case url(URL, MediaHostProtocol?)
case urlRequest(URLRequest)
var url: URL? {
switch self {
case .url(let url, _): url
case .urlRequest(let request): request.url
}
}
}
let source: Source
let options: ImageRequestOptions
public init(url: URL, host: MediaHostProtocol? = nil, options: ImageRequestOptions = .init()) {
self.source = .url(url, host)
self.options = options
}
public init(urlRequest: URLRequest, options: ImageRequestOptions = .init()) {
self.source = .urlRequest(urlRequest)
self.options = options
}
}
public struct ImageRequestOptions: Hashable, Sendable {
/// Resize the thumbnail to the given size. By default, `nil`.
public var size: ImageSize?
/// If enabled, uses ``MemoryCache`` for caching decompressed images.
public var isMemoryCacheEnabled = true
/// If enabled, uses `URLSession` preconfigured with a custom `URLCache`
/// with a relatively high disk capacity. By default, `true`.
public var isDiskCacheEnabled = true
public init(
size: ImageSize? = nil,
isMemoryCacheEnabled: Bool = true,
isDiskCacheEnabled: Bool = true
) {
self.size = size
self.isMemoryCacheEnabled = isMemoryCacheEnabled
self.isDiskCacheEnabled = isDiskCacheEnabled
}
}
/// Image size in **pixels**.
public struct ImageSize: Hashable, Sendable {
public let width: CGFloat
public let height: CGFloat
public init(width: CGFloat, height: CGFloat) {
self.width = width
self.height = height
}
public init(_ size: CGSize) {
self.width = size.width
self.height = size.height
}
/// Initializes `ImageSize` with the given size scaled for the given view.
@MainActor
public init(scaling size: CGSize, in view: UIView) {
self.init(size.scaled(by: view.traitCollection.displayScale))
}
/// Initializes `ImageSize` with the given size scaled for the current trait
/// collection display scale.
public init(scaling size: CGSize) {
self.init(size.scaled(by: UITraitCollection.current.displayScale))
}
}
extension CGSize {
init(_ size: ImageSize) {
self.init(width: size.width, height: size.height)
}
}