-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIImageView+Extensions.swift
More file actions
29 lines (26 loc) · 981 Bytes
/
UIImageView+Extensions.swift
File metadata and controls
29 lines (26 loc) · 981 Bytes
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
import UIKit
extension UIImageView {
private static var requestIdKey = "UIImageViewRequestID"
private var lastRequestedURL: String? {
get {
return objc_getAssociatedObject(self,
&UIImageView.requestIdKey) as? String
}
set {
objc_setAssociatedObject(self,
&UIImageView.requestIdKey,
newValue,
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func loadImage(at url: URL) {
lastRequestedURL = url.absoluteString
URLSession.shared.dataTask(with: url) { [weak self] (data, _, _) in
guard let data = data,
self?.lastRequestedURL == url.absoluteString else { return }
DispatchQueue.main.sync {
self?.image = UIImage(data: data)
}
}.resume()
}
}