-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathPAAImageView.m
307 lines (245 loc) · 11 KB
/
PAAImageView.m
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// PAAImageView.m
// ImageDL
//
// Created by Pierre Abi-aad on 21/03/2014.
// Copyright (c) 2014 Pierre Abi-aad. All rights reserved.
//
#import "PAAImageView.h"
#import "AFNetworking/AFNetworking.h"
#pragma mark - Utils
#define rad(degrees) ((degrees) / (180.0 / M_PI))
#define kLineWidth 3.f
NSString * const paa_identifier = @"paa.imagecache.tg";
#pragma mark - SPMImageCache interface
@interface SPMImageCache : NSObject
@property (nonatomic, strong) NSString *cachePath;
@property (nonatomic, strong) NSFileManager *fileManager;
- (void)setImage:(UIImage *)image forURL:(NSURL *)URL;
- (UIImage *)getImageForURL:(NSURL *)URL;
@end
#pragma mark - SPMImageAsyncView interface
@interface PAAImageView ()
@property (nonatomic, strong) CAShapeLayer *backgroundLayer;
@property (nonatomic, strong) CAShapeLayer *progressLayer;
@property (nonatomic, strong, readwrite) UIImageView *containerImageView;
@property (nonatomic, strong) UIView *progressContainer;
@property (nonatomic, strong) SPMImageCache *cache;
@end
#pragma mark - SPMImageAsyncView
@implementation PAAImageView
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self commonInitWithFrame:self.frame
backgroundProgressColor:[UIColor whiteColor]
progressColor:[UIColor blueColor]];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
return [[PAAImageView alloc] initWithFrame:frame
backgroundProgressColor:[UIColor whiteColor]
progressColor:[UIColor blueColor]];
}
- (id)initWithFrame:(CGRect)frame backgroundProgressColor:(UIColor *)backgroundProgresscolor progressColor:(UIColor *)progressColor
{
self = [super initWithFrame:frame];
if (self)
{
[self commonInitWithFrame:frame
backgroundProgressColor:backgroundProgresscolor
progressColor:progressColor];
}
return self;
}
- (void)commonInitWithFrame:(CGRect)frame backgroundProgressColor:(UIColor *)backgroundProgresscolor progressColor:(UIColor *)progressColor
{
_backgroundProgresscolor = backgroundProgresscolor;
_progressColor = progressColor;
self.layer.cornerRadius = CGRectGetWidth(self.bounds)/2.f;
self.layer.masksToBounds = NO;
self.clipsToBounds = YES;
self.cacheEnabled = YES;
self.cache = [[SPMImageCache alloc] init];
CGPoint arcCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
CGFloat radius = MIN(CGRectGetMidX(self.bounds) - 1, CGRectGetMidY(self.bounds)-1);
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:arcCenter
radius:radius
startAngle:-rad(90)
endAngle:rad(360-90)
clockwise:YES];
self.backgroundLayer = [CAShapeLayer layer];
self.backgroundLayer.path = circlePath.CGPath;
self.backgroundLayer.strokeColor = [backgroundProgresscolor CGColor];
self.backgroundLayer.fillColor = [[UIColor clearColor] CGColor];
self.backgroundLayer.lineWidth = kLineWidth;
self.progressLayer = [CAShapeLayer layer];
self.progressLayer.path = self.backgroundLayer.path;
self.progressLayer.strokeColor = [progressColor CGColor];
self.progressLayer.fillColor = self.backgroundLayer.fillColor;
self.progressLayer.lineWidth = self.backgroundLayer.lineWidth;
self.progressLayer.strokeEnd = 0.f;
self.progressContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.progressContainer.layer.cornerRadius = CGRectGetWidth(self.bounds)/2.f;
self.progressContainer.layer.masksToBounds = NO;
self.progressContainer.clipsToBounds = YES;
self.progressContainer.backgroundColor = [UIColor clearColor];
self.containerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(1, 1, frame.size.width-2, frame.size.height-2)];
self.containerImageView.layer.cornerRadius = CGRectGetWidth(self.bounds)/2.f;
self.containerImageView.layer.masksToBounds = NO;
self.containerImageView.clipsToBounds = YES;
self.containerImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.progressContainer.layer addSublayer:self.backgroundLayer];
[self.progressContainer.layer addSublayer:self.progressLayer];
[self addSubview:self.containerImageView];
[self addSubview:self.progressContainer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self addGestureRecognizer:tapRecognizer];
}
- (void)handleSingleTap:(id)sender
{
if (self.delegate && [self.delegate respondsToSelector:@selector(paaImageViewDidTapped:)])
{
[self.delegate paaImageViewDidTapped:self];
}
}
- (void)setBackgroundProgresscolor:(UIColor *)backgroundProgresscolor
{
_backgroundProgresscolor = backgroundProgresscolor;
self.backgroundLayer.strokeColor = [backgroundProgresscolor CGColor];
}
- (void)setProgressColor:(UIColor *)progressColor
{
_progressColor = progressColor;
self.progressLayer.strokeColor = [progressColor CGColor];
}
- (void)setPlaceHolderImage:(UIImage *)placeHolderImage
{
_placeHolderImage = placeHolderImage;
if (!self.containerImageView.image)
{
self.containerImageView.image = placeHolderImage;
}
}
- (void)setImageURL:(NSURL *)URL
{
[self setImageURL:URL completion:nil];
}
- (void)setImageURL:(NSURL *)URL completion:(void (^)(NSError *error))completionBlock {
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:URL];
UIImage *cachedImage = (self.cacheEnabled) ? [self.cache getImageForURL:URL] : nil;
if(cachedImage)
{
[self updateWithImage:cachedImage animated:NO];
}
else
{
__weak __typeof(self)weakSelf = self;
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
CGFloat progress = (CGFloat)totalBytesRead/(CGFloat)totalBytesExpectedToRead;
self.progressLayer.strokeEnd = progress;
self.backgroundLayer.strokeStart = progress;
}];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
UIImage *image = responseObject;
[weakSelf updateWithImage:image animated:YES];
if(self.cacheEnabled)
{
[self.cache setImage:responseObject forURL:URL];
}
if(completionBlock) completionBlock(nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if(completionBlock) completionBlock(error);
}];
[requestOperation start];
}
}
- (void)setImage:(UIImage *)image {
UIImage *cachedImage = image;
if (cachedImage) {
[self updateWithImage:cachedImage animated:NO];
}
}
- (void)setBackgroundWidth:(CGFloat)width {
self.backgroundLayer.lineWidth = width;
self.progressLayer.lineWidth = self.backgroundLayer.lineWidth;
}
- (void)updateWithImage:(UIImage *)image animated:(BOOL)animated
{
CGFloat duration = (animated) ? 0.3 : 0.f;
CGFloat delay = (animated) ? 0.1 : 0.f;
self.containerImageView.transform = CGAffineTransformMakeScale(0, 0);
self.containerImageView.alpha = 0.f;
self.containerImageView.image = image;
[UIView animateWithDuration:duration
animations:^{
self.progressContainer.transform = CGAffineTransformMakeScale(1.1, 1.1);
self.progressContainer.alpha = 0.f;
[UIView animateWithDuration:duration
delay:delay
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.containerImageView.transform = CGAffineTransformIdentity;
self.containerImageView.alpha = 1.f;
} completion:nil];
} completion:^(BOOL finished) {
self.progressLayer.strokeColor = [self.progressColor CGColor];
[UIView animateWithDuration:duration
animations:^{
self.progressContainer.transform = CGAffineTransformIdentity;
self.progressContainer.alpha = 1.f;
}];
}];
}
@end
#pragma mark - SPMImageCache
@implementation SPMImageCache
- (instancetype)init
{
self = [super init];
if(self)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *rootCachePath = [paths firstObject];
self.fileManager = [NSFileManager defaultManager];
self.cachePath = [rootCachePath stringByAppendingPathComponent:paa_identifier];
if(![self.fileManager fileExistsAtPath:paa_identifier])
{
[self.fileManager createDirectoryAtPath:self.cachePath withIntermediateDirectories:NO attributes:nil error:nil];
}
}
return self;
}
- (void)setImage:(UIImage *)image forURL:(NSURL *)URL
{
NSData *imageData = nil;
NSString *fileExtension = [URL pathExtension];//[[URL componentsSeparatedByString:@"."] lastObject];
if([fileExtension isEqualToString:@"png"])
{
imageData = UIImagePNGRepresentation(image);
}
else if([fileExtension isEqualToString:@"jpg"] || [fileExtension isEqualToString:@"jpeg"])
{
imageData = UIImageJPEGRepresentation(image, 1.f);
}
else
return;
[imageData writeToFile:[self.cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%lu.%@", (unsigned long)URL.hash, fileExtension]] atomically:YES];
}
- (UIImage *)getImageForURL:(NSURL *)URL
{
NSString *fileExtension = [URL pathExtension];//[[URL componentsSeparatedByString:@"."] lastObject];
NSString *path = [self.cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%lu.%@", URL.hash, fileExtension]];
if([self.fileManager fileExistsAtPath:path])
{
return [UIImage imageWithContentsOfFile:path];
}
return nil;
}
@end