-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathAsyncImageView.swift
195 lines (167 loc) · 6.06 KB
/
AsyncImageView.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import UIKit
import Gifu
/// A simple image view that supports rendering both static and animated images
/// (see ``AnimatedImage``).
@MainActor
public final class AsyncImageView: UIView {
private let imageView = GIFImageView()
private var errorView: UIImageView?
private var spinner: UIActivityIndicatorView?
private let controller = ImageLoadingController()
public enum LoadingStyle {
/// Shows a secondary background color during the download.
case background
/// Shows a spinner during the download.
case spinner
}
public struct Configuration {
/// Image tint color.
public var tintColor: UIColor?
/// Image view content mode.
public var contentMode: UIView.ContentMode?
/// Enabled by default and shows an error icon on failures.
public var isErrorViewEnabled = true
/// By default, `background`.
public var loadingStyle = LoadingStyle.background
// TODO: remove when WPRichTextImage is removed
public var passTouchesToSuperview = false
public init() {}
}
public var configuration = Configuration() {
didSet { didUpdateConfiguration(configuration) }
}
/// The currently displayed image. If the image is animated, returns an
/// instance of ``AnimatedImage``.
public var image: UIImage? {
didSet {
if let image {
imageView.configure(image: image)
} else {
imageView.prepareForReuse()
}
}
}
public override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupView() {
controller.onStateChanged = { [weak self] in self?.setState($0) }
addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: topAnchor),
imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: bottomAnchor),
imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
])
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
imageView.accessibilityIgnoresInvertColors = true
backgroundColor = .secondarySystemBackground
}
/// Removes the current image and stops the outstanding downloads.
public func prepareForReuse() {
controller.prepareForReuse()
image = nil
}
/// - parameter size: Target image size in pixels.
public func setImage(
with imageURL: URL,
host: MediaHostProtocol? = nil,
size: ImageSize? = nil
) {
let request = ImageRequest(url: imageURL, host: host, options: ImageRequestOptions(size: size))
controller.setImage(with: request)
}
public func setImage(with request: ImageRequest, completion: (@MainActor (Result<UIImage, Error>) -> Void)? = nil) {
controller.setImage(with: request, completion: completion)
}
private func setState(_ state: ImageLoadingController.State) {
imageView.isHidden = true
errorView?.isHidden = true
spinner?.stopAnimating()
switch state {
case .loading:
switch configuration.loadingStyle {
case .background:
backgroundColor = .secondarySystemBackground
case .spinner:
makeSpinner().startAnimating()
}
case .success(let image):
self.image = image
imageView.isHidden = false
backgroundColor = .clear
case .failure:
if configuration.isErrorViewEnabled {
makeErrorView().isHidden = false
}
}
}
private func didUpdateConfiguration(_ configuration: Configuration) {
if let tintColor = configuration.tintColor {
imageView.tintColor = tintColor
}
if let contentMode = configuration.contentMode {
imageView.contentMode = contentMode
}
}
private func makeSpinner() -> UIActivityIndicatorView {
if let spinner {
return spinner
}
let spinner = UIActivityIndicatorView()
addSubview(spinner)
spinner.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
spinner.centerXAnchor.constraint(equalTo: centerXAnchor),
spinner.centerYAnchor.constraint(equalTo: centerYAnchor)
])
self.spinner = spinner
return spinner
}
private func makeErrorView() -> UIImageView {
if let errorView {
return errorView
}
let errorView = UIImageView(image: UIImage(systemName: "exclamationmark.triangle"))
errorView.tintColor = .separator
addSubview(errorView)
errorView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
errorView.centerXAnchor.constraint(equalTo: centerXAnchor),
errorView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
self.errorView = errorView
return errorView
}
public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if configuration.passTouchesToSuperview && self.bounds.contains(point) {
// Pass the touch to the superview
return nil
}
return super.hitTest(point, with: event)
}
}
extension GIFImageView {
/// If the image is an instance of `AnimatedImage` type, plays it as an
/// animated image.
public func configure(image: UIImage) {
if let gif = image as? AnimatedImage, let data = gif.gifData {
self.animate(withGIFData: data)
} else {
self.image = image
}
}
public func prepareForReuse() {
if isAnimatingGIF {
prepareForReuse()
} else {
image = nil
}
}
}