-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUnsplashImageView.swift
154 lines (117 loc) · 5.07 KB
/
UnsplashImageView.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
import os
import UIKit
public class UnsplashImageView: UIImageView {
// MARK: - Public properties
@IBInspectable public var accessKey: String = ""
@IBInspectable public var query: String = ""
var imageURL: URL?
// MARK: - Public functions
public func fetchPhoto() {
if let imageURL = imageURL {
fetchImage(with: imageURL)
return
}
guard photoDataTask == nil, let urlRequest = photoURLRequest() else { return }
photoDataTask = URLSession.shared.dataTask(with: urlRequest) { [weak self] (data, _, error) in
guard let strongSelf = self else { return }
strongSelf.photoDataTask = nil
if let error = error { return os_log("%@", log: .default, type: .error, error.localizedDescription) }
guard let data = data, let imageURL = strongSelf.imageURL(from: data) else { return }
strongSelf.imageURL = imageURL
strongSelf.fetchImage(with: imageURL)
}
photoDataTask?.resume()
}
// MARK: - Private properties
private var photoDataTask: URLSessionDataTask?
private var imageDataTask: URLSessionDataTask?
private let apiEndpoint = "https://api.unsplash.com/photos/random"
private var screenScale: CGFloat { return window?.screen.scale ?? 2 } // Default to 2x
private static var cache = URLCache(memoryCapacity: 50.megabytes, diskCapacity: 100.megabytes, diskPath: "unsplash")
// MARK: - Private functions
private func fetchImage(with url: URL) {
guard imageDataTask == nil else { return }
if let cachedResponse = UnsplashImageView.cache.cachedResponse(for: URLRequest(url: url)),
let image = UIImage(data: cachedResponse.data) {
DispatchQueue.main.async {
self.image = image
}
return
}
imageDataTask = URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in
guard let strongSelf = self else { return }
strongSelf.imageDataTask = nil
if let error = error { return os_log("%@", log: .default, type: .error, error.localizedDescription) }
guard let data = data, let response = response, let image = UIImage(data: data) else { return }
let cachedResponse = CachedURLResponse(response: response, data: data)
UnsplashImageView.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url))
DispatchQueue.main.async {
UIView.transition(with: strongSelf, duration: 0.25, options: [.transitionCrossDissolve], animations: {
strongSelf.image = image
}, completion: nil)
}
}
imageDataTask?.resume()
}
private func photoURLRequest() -> URLRequest? {
assert(accessKey.isEmpty == false, "The Unsplash API access key is missing.")
guard var components = URLComponents(string: apiEndpoint) else { return nil }
components.queryItems = [
URLQueryItem(name: "query", value: query)
]
guard let url = components.url else { return nil }
var request = URLRequest(url: url)
request.addValue("Client-ID \(accessKey)", forHTTPHeaderField: "Authorization")
return request
}
private func imageURL(from data: Data) -> URL? {
do {
if
let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let urls = jsonObject["urls"] as? [String: Any],
let urlString = urls["full"] as? String,
let url = URL(string: urlString) {
return sizedImageURL(from: url)
}
} catch {
os_log("%@", log: .default, type: .error, error.localizedDescription)
}
return nil
}
private func sizedImageURL(from url: URL) -> URL {
var width: CGFloat = 0
var height: CGFloat = 0
DispatchQueue.main.sync {
width = frame.width * screenScale
height = frame.height * screenScale
}
return url.appending(queryItems: [
URLQueryItem(name: "max-w", value: "\(width)"),
URLQueryItem(name: "max-h", value: "\(height)")
])
}
}
// MARK: - URL extension
private extension URL {
func appending(queryItems: [URLQueryItem]) -> URL {
guard let components = URLComponents(url: self, resolvingAgainstBaseURL: true) else {
return self
}
var queryDictionary = [String: String]()
if let queryItems = components.queryItems {
for item in queryItems {
queryDictionary[item.name] = item.value
}
}
for item in queryItems {
queryDictionary[item.name] = item.value
}
var newComponents = components
newComponents.queryItems = queryDictionary.map({ URLQueryItem(name: $0.key, value: $0.value) })
return newComponents.url ?? self
}
}
// MARK: - Int extension
private extension Int {
var megabytes: Int { return self * 1024 * 1024 }
}