-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPASImageView.swift
More file actions
executable file
·276 lines (210 loc) · 10.8 KB
/
PASImageView.swift
File metadata and controls
executable file
·276 lines (210 loc) · 10.8 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// PASImageView.swift
// PASImageView
//
// Created by Pierre Abi-aad on 09/06/2014.
// Copyright (c) 2014 Pierre Abi-aad. All rights reserved.
//
import UIKit
import QuartzCore
let spm_identifier = "pas.imagecache.tg"
let kLineWidth :CGFloat = 3.0
func rad(degrees : Float) -> Float {
return ((degrees) / Float((180.0/M_PI)))
}
final class ImageCache : NSObject {
var cachePath: URL?
let fileManager = FileManager.default
override init() {
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let rootCachePath = paths[0]
cachePath = URL(string: rootCachePath)?.appendingPathComponent(spm_identifier)
if let path = cachePath, !fileManager.fileExists(atPath:path.absoluteString) {
do {
try fileManager.createDirectory(atPath: path.absoluteString, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
print(error)
}
}
super.init()
}
func save(image: UIImage?, atURL URL: URL) {
guard let image = image else { return }
let fileExtension = URL.pathExtension
var data : NSData?
if fileExtension == "png" {
data = UIImagePNGRepresentation(image) as NSData?
} else if fileExtension == "jpg" || fileExtension == "jpeg" {
data = UIImageJPEGRepresentation(image, 1.0) as NSData?
}
guard let imageData = data, let cachePath = self.cachePath else { return }
imageData.write(toFile: cachePath.appendingPathComponent(String(format: "%u.%@", URL.absoluteString.hash, fileExtension)).absoluteString, atomically: true)
}
func image(forURL URL: URL) -> UIImage? {
if let path = self.cachePath?.appendingPathComponent(String(format: "%u.%@", URL.absoluteString.hash, URL.pathExtension)),
self.fileManager.fileExists(atPath: path.absoluteString) {
if let data = NSData(contentsOfFile: path.absoluteString) {
return UIImage(data: data as Data)
}
}
return nil
}
}
@IBDesignable
public class PASImageView : UIView, URLSessionDownloadDelegate {
@IBInspectable public var backgroundProgressColor : UIColor = UIColor.black {
didSet { backgroundLayer.strokeColor = backgroundProgressColor.cgColor }
}
@IBInspectable public var progressColor : UIColor = UIColor.red {
didSet { progressLayer.strokeColor = progressColor.cgColor }
}
@IBInspectable public var placeHolderImage : UIImage? {
didSet {
if containerImageView.image == nil {
containerImageView.image = placeHolderImage
}
}
}
public var cacheEnabled = true
public var delegate :PASImageViewDelegate?
private var backgroundLayer = CAShapeLayer()
private var progressLayer = CAShapeLayer()
private var containerImageView = UIImageView()
private var progressContainer = UIView()
private var cache = ImageCache()
//MARK:- Initialization implemention
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupView()
}
public convenience init(frame: CGRect, delegate: PASImageViewDelegate) {
self.init(frame: frame)
self.delegate = delegate
}
public override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
}
override public func layoutSubviews() {
self.layer.cornerRadius = self.bounds.width/2.0
progressContainer.layer.cornerRadius = self.bounds.width/2.0
containerImageView.layer.cornerRadius = self.bounds.width/2.0
updatePaths()
}
//MARK:- Action
func handleSingleTap(gesture: UIGestureRecognizer) {
delegate?.PAImageView(didTapped: self)
}
//MARK:- Private methods
private func setConstraints() {
// containerImageView
containerImageView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraint(NSLayoutConstraint(item: containerImageView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: -2))
self.addConstraint(NSLayoutConstraint(item: containerImageView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1, constant: -2))
self.addConstraint(NSLayoutConstraint(item: containerImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: containerImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
// progressContainer
progressContainer.translatesAutoresizingMaskIntoConstraints = false
self.addConstraint(NSLayoutConstraint(item: progressContainer, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: progressContainer, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: progressContainer, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: progressContainer, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
}
private func setupView() {
self.layer.masksToBounds = false
self.clipsToBounds = true
updatePaths()
backgroundLayer.strokeColor = backgroundProgressColor.cgColor
backgroundLayer.fillColor = UIColor.clear.cgColor
backgroundLayer.lineWidth = kLineWidth
progressLayer.strokeColor = progressColor.cgColor
progressLayer.fillColor = backgroundLayer.fillColor
progressLayer.lineWidth = backgroundLayer.lineWidth
progressLayer.strokeEnd = 0.0
progressContainer = UIView()
progressContainer.layer.masksToBounds = false
progressContainer.clipsToBounds = true
progressContainer.backgroundColor = UIColor.clear
containerImageView = UIImageView()
containerImageView.layer.masksToBounds = false
containerImageView.clipsToBounds = true
containerImageView.contentMode = .scaleAspectFill
progressContainer.layer.addSublayer(backgroundLayer)
progressContainer.layer.addSublayer(progressLayer)
self.addSubview(containerImageView)
self.addSubview(progressContainer)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleSingleTap(gesture:))))
setConstraints()
}
private func update(image: UIImage?, animated: Bool) {
let duration = (animated) ? 0.3 : 0.0
let delay = (animated) ? 0.1 : 0.0
containerImageView.transform = CGAffineTransform(scaleX: 0, y: 0)
containerImageView.alpha = 0.0
if let image = image {
containerImageView.image = image
}
UIView.animate(withDuration: duration, animations: {
self.progressContainer.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
self.progressContainer.alpha = 0.0
UIView.animate(withDuration: duration, delay: delay, options: [.curveEaseOut], animations: {
self.containerImageView.transform = CGAffineTransform.identity
self.containerImageView.alpha = 1.0
}, completion: nil)
}, completion: { finished in
self.progressLayer.strokeColor = self.backgroundProgressColor.cgColor
UIView.animate(withDuration: duration, animations: {
self.progressContainer.transform = CGAffineTransform.identity
self.progressContainer.alpha = 1.0
})
})
}
private func updatePaths() {
let arcCenter = CGPoint(x: self.bounds.midX, y: self.bounds.midY)
let radius = Float(min(self.bounds.midX - 1, self.bounds.midY-1))
let circlePath = UIBezierPath(arcCenter: arcCenter,
radius: CGFloat(radius),
startAngle: CGFloat(-rad(degrees: Float(90))),
endAngle: CGFloat(rad(degrees: 360-90)),
clockwise: true)
backgroundLayer.path = circlePath.cgPath
progressLayer.path = backgroundLayer.path
}
//MARK:- Public methods
public func imageURL(URL: URL) {
let urlRequest = URLRequest(url: URL)
let cachedImage = (cacheEnabled) ? cache.image(forURL: URL) : nil
if (cachedImage != nil) {
update(image: cachedImage!, animated: false)
} else {
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
let downloadTask = session.downloadTask(with: urlRequest)
downloadTask.resume()
}
}
//MARK:- URLSessionDownloadDelegate implemention
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
if let data = try? Data(contentsOf: location) {
let image = UIImage(data: data)
DispatchQueue.main.async {
self.update(image: image , animated: true)
}
if cacheEnabled {
if let url = downloadTask.response?.url {
cache.save(image: image, atURL: url)
}
}
}
}
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress: CGFloat = CGFloat(totalBytesWritten)/CGFloat(totalBytesExpectedToWrite)
DispatchQueue.main.async {
self.progressLayer.strokeEnd = progress
self.backgroundLayer.strokeStart = progress
}
}
}
//MARK:- PASImageViewDelegate
public protocol PASImageViewDelegate {
func PAImageView(didTapped imageView: PASImageView)
}