-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUIImageLoader.m
executable file
·767 lines (633 loc) · 25.5 KB
/
UIImageLoader.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
#import "UIImageLoader.h"
#import <objc/runtime.h>
/* UIImageMemoryCache */
@interface UIImageMemoryCache ()
@property NSCache * cache;
@end
@implementation UIImageMemoryCache
- (id) init {
self = [super init];
self.cache = [[NSCache alloc] init];
self.cache.totalCostLimit = 25 * (1024 * 1024); //25MB
return self;
}
- (void) cacheImage:(UIImageLoaderImage *) image forURL:(NSURL *) url; {
if(image) {
NSUInteger cost = (image.size.width * image.size.height) * 4;
[self.cache setObject:image forKey:url.path cost:cost];
}
}
- (void) removeImageForURL:(NSURL *) url; {
[self.cache removeObjectForKey:url.path];
}
- (void) purge; {
[self.cache removeAllObjects];
}
@end
/* UIImageCacheData */
@interface UIImageCacheData : NSObject <NSCoding>
@property NSTimeInterval maxage;
@property NSString * etag;
@property NSString * lastModified;
@property BOOL nocache;
//for errors 4XX,5XX
@property NSInteger errorAttempts;
@property NSTimeInterval errorMaxage;
@property NSError * errorLast;
@end
/* UIImageLoader */
typedef void(^UIImageLoadedBlock)(UIImageLoaderImage * image);
typedef void(^NSURLAndDataWriteBlock)(NSURL * url, NSData * data);
typedef void(^UIImageLoaderURLCompletion)(NSError * error, NSURL * diskURL, UIImageLoadSource loadedFromSource);
typedef void(^UIImageLoaderDiskURLCompletion)(NSURL * diskURL);
//errors
NSString * const UIImageLoaderErrorDomain = @"com.gngrwzrd.UIImageLoader";
const NSInteger UIImageLoaderErrorNilURL = 1;
//default loader
static UIImageLoader * _default;
//private loader properties
@interface UIImageLoader ()
@property NSURLSession * activeSession;
@property NSURL * activeCacheDirectory;
@property NSString * auth;
@end
/* UIImageLoader */
@implementation UIImageLoader
+ (UIImageLoader *) defaultLoader {
if(!_default) {
_default = [[UIImageLoader alloc] init];
}
return _default;
}
- (id) init {
NSURL * appSupport = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
NSString * bundleId = [[NSBundle mainBundle] infoDictionary][@"CFBundleIdentifier"];
NSURL * defaultCacheDir = appSupport;
if(bundleId) {
defaultCacheDir = [defaultCacheDir URLByAppendingPathComponent:bundleId];
}
defaultCacheDir = [defaultCacheDir URLByAppendingPathComponent:@"UIImageLoader"];
self = [self initWithCacheDirectory:defaultCacheDir];
return self;
}
- (id) initWithCacheDirectory:(NSURL *) url; {
self = [super init];
self.cacheImagesInMemory = FALSE;
self.trustAnySSLCertificate = FALSE;
self.useServerCachePolicy = TRUE;
self.logCacheMisses = TRUE;
self.defaultCacheControlMaxAge = 0;
self.memoryCache = [[UIImageMemoryCache alloc] init];
self.cacheDirectory = url;
self.defaultCacheControlMaxAgeForErrors = 0;
self.maxAttemptsForErrors = 0;
return self;
}
- (void) setCacheDirectory:(NSURL *) cacheDirectory {
self.activeCacheDirectory = cacheDirectory;
[[NSFileManager defaultManager] createDirectoryAtURL:cacheDirectory withIntermediateDirectories:TRUE attributes:nil error:nil];
}
- (NSURL *) cacheDirectory {
return self.activeCacheDirectory;
}
- (void) setAuthUsername:(NSString *) username password:(NSString *) password; {
if(username == nil || password == nil) {
self.auth = nil;
return;
}
NSString * authString = [NSString stringWithFormat:@"%@:%@",username,password];
NSData * authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
NSString * encoded = [authData base64EncodedStringWithOptions:0];
self.auth = [NSString stringWithFormat:@"Basic %@",encoded];
}
- (void) setAuthorization:(NSMutableURLRequest *) request {
if(self.auth) {
[request setValue:self.auth forHTTPHeaderField:@"Authorization"];
}
}
- (void) clearCachedFilesModifiedOlderThan1Day; {
[self clearCachedFilesModifiedOlderThan:86400];
}
- (void) clearCachedFilesModifiedOlderThan1Week; {
[self clearCachedFilesModifiedOlderThan:604800];
}
- (void) clearCachedFilesModifiedOlderThan:(NSTimeInterval) timeInterval; {
dispatch_queue_t background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(background, ^{
NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.cacheDirectory.path error:nil];
for(NSString * file in files) {
NSURL * path = [self.cacheDirectory URLByAppendingPathComponent:file];
NSDictionary * attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path.path error:nil];
NSDate * modified = attributes[NSFileModificationDate];
NSTimeInterval diff = [[NSDate date] timeIntervalSinceDate:modified];
if(diff > timeInterval) {
NSLog(@"deleting cached file: %@",path.path);
[[NSFileManager defaultManager] removeItemAtPath:path.path error:nil];
}
}
});
}
- (void) clearCachedFilesCreatedOlderThan1Day; {
[self clearCachedFilesCreatedOlderThan:86400];
}
- (void) clearCachedFilesCreatedOlderThan1Week; {
[self clearCachedFilesCreatedOlderThan:604800];
}
- (void) clearCachedFilesCreatedOlderThan:(NSTimeInterval) timeInterval; {
dispatch_queue_t background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(background, ^{
NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.cacheDirectory.path error:nil];
for(NSString * file in files) {
NSURL * path = [self.cacheDirectory URLByAppendingPathComponent:file];
NSDictionary * attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path.path error:nil];
NSDate * created = attributes[NSFileCreationDate];
NSTimeInterval diff = [[NSDate date] timeIntervalSinceDate:created];
if(diff > timeInterval) {
NSLog(@"deleting cached file: %@",path.path);
[[NSFileManager defaultManager] removeItemAtPath:path.path error:nil];
}
}
});
}
- (void) purgeDiskCache; {
dispatch_queue_t background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(background, ^{
NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.cacheDirectory.path error:nil];
for(NSString * file in files) {
NSURL * path = [self.cacheDirectory URLByAppendingPathComponent:file];
NSLog(@"deleting cached file: %@",path.path);
[[NSFileManager defaultManager] removeItemAtPath:path.path error:nil];
}
});
}
- (void) purgeMemoryCache; {
[self.memoryCache purge];
}
- (void) setMemoryCacheMaxBytes:(NSUInteger) maxBytes; {
self.memoryCache.maxBytes = maxBytes;
}
- (void) setSession:(NSURLSession *) session {
self.activeSession = session;
if(session.delegate && self.trustAnySSLCertificate) {
if(![session.delegate respondsToSelector:@selector(URLSession:didReceiveChallenge:completionHandler:)]) {
NSLog(@"[UIImageLoader] WARNING: You set a custom NSURLSession and require trustAnySSLCertificate but your "
@"session delegate doesn't respond to URLSession:didReceiveChallenge:completionHandler:");
}
}
}
- (NSURLSession *) session {
if(self.activeSession) {
return self.activeSession;
}
NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
self.activeSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
return self.activeSession;
}
- (void) URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
if(self.trustAnySSLCertificate) {
completionHandler(NSURLSessionAuthChallengeUseCredential,[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
} else {
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling,nil);
}
}
- (NSURL *) localFileURLForURL:(NSURL *) url {
if(!url) {
return NULL;
}
NSString * path = [url.absoluteString stringByRemovingPercentEncoding];
NSString * path2 = [path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
path2 = [path2 stringByReplacingOccurrencesOfString:@"https://" withString:@""];
path2 = [path2 stringByReplacingOccurrencesOfString:@":" withString:@"-"];
path2 = [path2 stringByReplacingOccurrencesOfString:@"?" withString:@"-"];
path2 = [path2 stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
path2 = [path2 stringByReplacingOccurrencesOfString:@" " withString:@"_"];
return [self.cacheDirectory URLByAppendingPathComponent:path2];
}
- (NSURL *) localCacheControlFileURLForURL:(NSURL *) url {
if(!url) {
return NULL;
}
NSURL * localImageFile = [self localFileURLForURL:url];
NSString * path = [localImageFile.path stringByAppendingString:@".cc"];
return [NSURL fileURLWithPath:path];
}
- (NSDate *) createdDateForFileURL:(NSURL *) url {
NSDictionary * attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:url.path error:nil];
if(!attributes) {
return nil;
}
return attributes[NSFileCreationDate];
}
- (void) writeData:(NSData *) data toFile:(NSURL *) cachedURL writeCompletion:(NSURLAndDataWriteBlock) writeCompletion {
dispatch_queue_t background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(background, ^{
[data writeToFile:cachedURL.path atomically:TRUE];
if(writeCompletion) {
writeCompletion(cachedURL,data);
}
});
}
- (void) writeCacheControlData:(UIImageCacheData *) cache toFile:(NSURL *) cachedURL {
dispatch_queue_t background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(background, ^{
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:cache];
[data writeToFile:cachedURL.path atomically:TRUE];
NSDictionary * attributes = @{NSFileModificationDate:[NSDate date]};
[[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:cachedURL.path error:nil];
});
}
- (void) loadImageInBackground:(NSURL *) diskURL completion:(UIImageLoadedBlock) completion {
dispatch_queue_t background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(background, ^{
NSDate * modified = [NSDate date];
NSDictionary * attributes = @{NSFileModificationDate:modified};
[[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:diskURL.path error:nil];
NSURL * cachedInfoFile = [diskURL URLByAppendingPathExtension:@"cc"];
[[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:cachedInfoFile.path error:nil];
UIImageLoaderImage * image = [[UIImageLoaderImage alloc] initWithContentsOfFile:diskURL.path];
if(completion) {
completion(image);
}
});
}
- (void) setCacheControlForCacheInfo:(UIImageCacheData *) cacheInfo fromCacheControlString:(NSString *) cacheControl {
if([cacheControl isEqualToString:@"no-cache"]) {
cacheInfo.nocache = TRUE;
return;
}
NSScanner * scanner = [[NSScanner alloc] initWithString:cacheControl];
NSString * prelim = nil;
[scanner scanUpToString:@"=" intoString:&prelim];
[scanner scanString:@"=" intoString:nil];
double maxage = -1;
[scanner scanDouble:&maxage];
if(maxage > -1) {
cacheInfo.maxage = (NSTimeInterval)maxage;
}
}
- (NSURLSessionDataTask *) cacheImageWithRequestUsingCacheControl:(NSURLRequest *) request
hasCache:(UIImageLoaderDiskURLCompletion) hasCache
sendingRequest:(UIImageLoader_SendingRequestBlock) sendingRequest
requestCompleted:(UIImageLoaderURLCompletion) requestCompleted {
if(!request.URL || request.URL.absoluteString.length < 1) {
requestCompleted([NSError errorWithDomain:UIImageLoaderErrorDomain code:UIImageLoaderErrorNilURL userInfo:@{NSLocalizedDescriptionKey:@"The request URL is nil or empty."}],nil,UIImageLoadSourceNone);
}
//make mutable request
NSMutableURLRequest * mutableRequest = [request mutableCopy];
[self setAuthorization:mutableRequest];
//get cache file urls
NSURL * cacheInfoFile = [self localCacheControlFileURLForURL:request.URL];
NSURL * cachedImageURL = [self localFileURLForURL:request.URL];
//setup blank cache object
UIImageCacheData * cached = nil;
//load cached info file if it exists.
if([[NSFileManager defaultManager] fileExistsAtPath:cacheInfoFile.path]) {
cached = [NSKeyedUnarchiver unarchiveObjectWithFile:cacheInfoFile.path];
} else {
cached = [[UIImageCacheData alloc] init];
}
//check max age
NSDate * now = [NSDate date];
NSDate * createdDate = [self createdDateForFileURL:cachedImageURL];
NSTimeInterval diff = [now timeIntervalSinceDate:createdDate];
BOOL cacheValid = FALSE;
//check cache expiration
if(!cached.nocache && cached.maxage > 0 && diff < cached.maxage) {
cacheValid = TRUE;
}
//check error attempts and max error age
if(cached.errorLast) {
NSDate * cacheInfoFileCreatedDate = [self createdDateForFileURL:cacheInfoFile];
NSTimeInterval errorDiff = [now timeIntervalSinceDate:cacheInfoFileCreatedDate];
if(!cached.nocache && cached.errorAttempts >= self.maxAttemptsForErrors && cached.errorMaxage > 0 && errorDiff < cached.errorMaxage) {
requestCompleted(cached.errorLast,nil,UIImageLoadSourceNone);
return nil;
}
}
BOOL didSendCacheCompletion = FALSE;
//file exists.
if([[NSFileManager defaultManager] fileExistsAtPath:cachedImageURL.path]) {
if(cacheValid) {
hasCache(cachedImageURL);
return nil;
} else {
didSendCacheCompletion = TRUE;
//call hasCache completion and continue load below
hasCache(cachedImageURL);
}
} else {
if(self.logCacheMisses) {
NSLog(@"[UIImageLoader] cache miss for url: %@",request.URL);
}
}
//ignore built in cache from networking code. handled here instead.
mutableRequest.cachePolicy = NSURLRequestReloadIgnoringCacheData;
//add etag if available
if(cached.etag) {
[mutableRequest setValue:cached.etag forHTTPHeaderField:@"If-None-Match"];
}
//add last modified if available
if(cached.lastModified) {
[mutableRequest setValue:cached.lastModified forHTTPHeaderField:@"If-Modified-Since"];
}
//reset error cache info file if necessary.
if(cached.errorLast) {
cached.errorLast = nil;
}
if(cached.errorAttempts >= self.maxAttemptsForErrors) {
cached.errorAttempts = 0;
}
sendingRequest(didSendCacheCompletion);
NSURLSessionDataTask * task = [[self session] dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
NSDictionary * headers = [httpResponse allHeaderFields];
//304 Not Modified use cache
if(httpResponse.statusCode == 304) {
if(headers[@"Cache-Control"]) {
[self setCacheControlForCacheInfo:cached fromCacheControlString:headers[@"Cache-Control"]];
[self writeCacheControlData:cached toFile:cacheInfoFile];
} else {
cached.maxage = self.defaultCacheControlMaxAge;
[self writeCacheControlData:cached toFile:cacheInfoFile];
}
requestCompleted(nil,cachedImageURL,UIImageLoadSourceNetworkNotModified);
return;
}
//4XX, 5XX errors we can possibly cache.
if(httpResponse.statusCode > 399 && httpResponse.statusCode < 600) {
NSString * errorString = [NSString stringWithFormat:@"Request failed with error code %li", (long)httpResponse.statusCode];
NSDictionary * info = @{NSLocalizedDescriptionKey:errorString};
NSError * error = [[NSError alloc] initWithDomain:UIImageLoaderErrorDomain code:httpResponse.statusCode userInfo:info];
if(self.defaultCacheControlMaxAgeForErrors > 0) {
cached.errorAttempts++;
}
cached.errorLast = error;
cached.errorMaxage = self.defaultCacheControlMaxAgeForErrors;
[self writeCacheControlData:cached toFile:cacheInfoFile];
requestCompleted(error,nil,UIImageLoadSourceNone);
return;
}
//error
if(httpResponse.statusCode < 200 && httpResponse.statusCode > 299 && error) {
requestCompleted(error,nil,UIImageLoadSourceNone);
return;
}
//check for Cache-Control
if(headers[@"Cache-Control"]) {
[self setCacheControlForCacheInfo:cached fromCacheControlString:headers[@"Cache-Control"]];
} else {
cached.maxage = self.defaultCacheControlMaxAge;
}
//check for ETag
if(headers[@"ETag"]) {
cached.etag = headers[@"ETag"];
}
//check for Last Modified
if(headers[@"Last-Modified"]) {
cached.lastModified = headers[@"Last-Modified"];
}
//save cached info file
[self writeCacheControlData:cached toFile:cacheInfoFile];
//save image to disk
[self writeData:data toFile:cachedImageURL writeCompletion:^(NSURL *url, NSData *data) {
requestCompleted(nil,cachedImageURL,UIImageLoadSourceNetworkToDisk);
}];
}];
[task resume];
return task;
}
- (NSURLSessionDataTask *) cacheImageWithRequest:(NSURLRequest *) request
hasCache:(UIImageLoaderDiskURLCompletion) hasCache
sendingRequest:(UIImageLoader_SendingRequestBlock) sendingRequest
requestComplete:(UIImageLoaderURLCompletion) requestComplete {
//if use server cache policies, use other method.
if(self.useServerCachePolicy) {
return [self cacheImageWithRequestUsingCacheControl:request hasCache:hasCache sendingRequest:sendingRequest requestCompleted:requestComplete];
}
if(!request.URL || request.URL.absoluteString.length < 1) {
requestComplete([NSError errorWithDomain:UIImageLoaderErrorDomain code:UIImageLoaderErrorNilURL userInfo:@{NSLocalizedDescriptionKey:@"The request URL is nil or empty."}],nil,UIImageLoadSourceNone);
}
//make mutable request
NSMutableURLRequest * mutableRequest = [request mutableCopy];
[self setAuthorization:mutableRequest];
NSURL * cachedURL = [self localFileURLForURL:mutableRequest.URL];
if([[NSFileManager defaultManager] fileExistsAtPath:cachedURL.path]) {
hasCache(cachedURL);
return nil;
}
if(self.logCacheMisses) {
NSLog(@"[UIImageLoader] cache miss for url: %@",mutableRequest.URL);
}
sendingRequest(FALSE);
NSURLSessionDataTask * task = [[self session] dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(error) {
requestComplete(error,nil,UIImageLoadSourceNone);
return;
}
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
if(httpResponse.statusCode != 200) {
requestComplete(error,nil,UIImageLoadSourceNone);
return;
}
if(data) {
[self writeData:data toFile:cachedURL writeCompletion:^(NSURL *url, NSData *data) {
requestComplete(nil,cachedURL,UIImageLoadSourceNetworkToDisk);
}];
}
}];
[task resume];
return task;
}
- (NSURLSessionDataTask *) loadImageWithRequest:(NSURLRequest *) request
hasCache:(UIImageLoader_HasCacheBlock) hasCache
sendingRequest:(UIImageLoader_SendingRequestBlock) sendingRequest
requestCompleted:(UIImageLoader_RequestCompletedBlock) requestCompleted; {
//check memory cache
UIImageLoaderImage * image = [self.memoryCache.cache objectForKey:request.URL.path];
if(image) {
dispatch_async(dispatch_get_main_queue(), ^{
hasCache(image,UIImageLoadSourceMemory);
});
return nil;
}
return [self cacheImageWithRequest:request hasCache:^(NSURL *diskURL) {
[self loadImageInBackground:diskURL completion:^(UIImageLoaderImage *image) {
if(self.cacheImagesInMemory) {
[self.memoryCache cacheImage:image forURL:request.URL];
}
dispatch_async(dispatch_get_main_queue(), ^{
hasCache(image,UIImageLoadSourceDisk);
});
}];
} sendingRequest:^(BOOL didHaveCache) {
dispatch_async(dispatch_get_main_queue(), ^{
sendingRequest(didHaveCache);
});
} requestComplete:^(NSError *error, NSURL *diskURL, UIImageLoadSource loadedFromSource) {
if(loadedFromSource == UIImageLoadSourceNetworkToDisk) {
[self loadImageInBackground:diskURL completion:^(UIImageLoaderImage *image) {
if(self.cacheImagesInMemory) {
[self.memoryCache cacheImage:image forURL:request.URL];
}
dispatch_async(dispatch_get_main_queue(), ^{
requestCompleted(error,image,loadedFromSource);
});
}];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
requestCompleted(error,nil,loadedFromSource);
});
}
}];
}
- (NSURLSessionDataTask *) loadImageWithURL:(NSURL *) url
hasCache:(UIImageLoader_HasCacheBlock) hasCache
sendingRequest:(UIImageLoader_SendingRequestBlock) sendingRequest
requestCompleted:(UIImageLoader_RequestCompletedBlock) requestCompleted; {
NSURLRequest * request = [NSURLRequest requestWithURL:url];
return [self loadImageWithRequest:request hasCache:hasCache sendingRequest:sendingRequest requestCompleted:requestCompleted];
}
@end
/********************/
/* UIImageCacheData */
/********************/
@implementation UIImageCacheData
- (id) init {
self = [super init];
self.maxage = 0;
self.etag = nil;
self.lastModified = nil;
self.errorMaxage = 0;
self.errorLast = nil;
self.errorAttempts = 0;
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super init];
NSKeyedUnarchiver * un = (NSKeyedUnarchiver *)aDecoder;
self.maxage = [un decodeDoubleForKey:@"maxage"];
self.etag = [un decodeObjectForKey:@"etag"];
self.nocache = [un decodeBoolForKey:@"nocache"];
self.lastModified = [un decodeObjectForKey:@"lastModified"];
self.errorLast = [un decodeObjectForKey:@"errorLast"];
self.errorMaxage = [un decodeDoubleForKey:@"errorMaxage"];
self.errorAttempts = [un decodeIntegerForKey:@"errorAttempts"];
return self;
}
- (void) encodeWithCoder:(NSCoder *)aCoder {
NSKeyedArchiver * ar = (NSKeyedArchiver *)aCoder;
[ar encodeObject:self.etag forKey:@"etag"];
[ar encodeDouble:self.maxage forKey:@"maxage"];
[ar encodeBool:self.nocache forKey:@"nocache"];
[ar encodeObject:self.lastModified forKey:@"lastModified"];
[ar encodeInteger:self.errorAttempts forKey:@"errorAttempts"];
[ar encodeObject:self.errorLast forKey:@"errorLast"];
[ar encodeDouble:self.errorMaxage forKey:@"errorMaxage"];
}
@end
#if TARGET_OS_IOS || TARGET_OS_TV
@implementation UIImageView (UIImageLoader)
#elif TARGET_OS_OSX
@implementation NSImageView (UIImageLoader)
#endif
static const char * _loadingURL = "uiImageLoader_loadingURL";
static const char * _runningTask = "uiImageLoader_runningTask";
static const char * _cancelsRunningTask = "uiImageLoader_cancelsRunningTask";
static const char * _finalScaling = "uiImageLoader_finalScaling";
static const char * _spinner = "uiImageLoader_spinner";
#if TARGET_OS_IOS || TARGET_OS_TV
- (void) uiImageLoader_setFinalContentMode:(UIViewContentMode) finalContentMode; {
objc_setAssociatedObject(self, _finalScaling, [NSNumber numberWithInt:finalContentMode], OBJC_ASSOCIATION_ASSIGN);
}
#elif TARGET_OS_OSX
- (void) uiImageLoader_setFinalImageScaling:(NSImageScaling) imageScaling; {
objc_setAssociatedObject(self, _finalScaling, [NSNumber numberWithInt:imageScaling], OBJC_ASSOCIATION_ASSIGN);
}
#endif
- (void) uiImageLoader_setCancelsRunningTask:(BOOL) cancelsRunningTask;{
objc_setAssociatedObject(self, _cancelsRunningTask, [NSNumber numberWithBool:cancelsRunningTask], OBJC_ASSOCIATION_ASSIGN);
}
- (void) uiImageLoader_setSpinner:(UIImageLoaderSpinner *) spinner; {
objc_setAssociatedObject(self, _spinner, spinner, OBJC_ASSOCIATION_ASSIGN);
}
- (void) uiImageLoader_setImageWithURL:(NSURL *) url; {
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[self uiImageLoader_setImageWithRequest:request];
}
- (void) uiImageLoader_setImageWithRequest:(NSURLRequest *) request; {
if(!request.URL || [request.URL.absoluteString length] < 1) {
return;
}
//get cancels task
BOOL cancelsTasks = [objc_getAssociatedObject(self, _cancelsRunningTask) boolValue];
//check if there's an existing task to cancel.
NSURLSessionDataTask * task = (NSURLSessionDataTask *)objc_getAssociatedObject(self, _runningTask);
if(task && cancelsTasks) {
[task cancel];
}
//get spinner
UIImageLoaderSpinner * spinner = objc_getAssociatedObject(self, _spinner);
//set the last requested URL to load.
objc_setAssociatedObject(self, _loadingURL, request.URL, OBJC_ASSOCIATION_COPY_NONATOMIC);
//load image.
task = [[UIImageLoader defaultLoader] loadImageWithRequest:request hasCache:^(UIImageLoaderImage * _Nullable image, UIImageLoadSource loadedFromSource) {
if(image) {
NSNumber * completedImageScaling = objc_getAssociatedObject(self, _finalScaling);
if(completedImageScaling) {
#if TARGET_OS_IOS || TARGET_OS_TV
self.contentMode = completedImageScaling.integerValue;
#elif TARGET_OS_OSX
[self setImageScaling:completedImageScaling.integerValue];
#endif
}
self.image = image;
}
if(spinner) {
#if TARGET_OS_IOS || TARGET_OS_TV
[spinner setHidden:YES];
[spinner stopAnimating];
#elif TARGET_OS_OSX
[spinner setHidden:YES];
[spinner stopAnimation:nil];
#endif
}
} sendingRequest:^(BOOL didHaveCachedImage) {
if(spinner && !didHaveCachedImage) {
#if TARGET_OS_IOS || TARGET_OS_TV
[spinner setHidden:NO];
[spinner startAnimating];
#elif TARGET_OS_OSX
[spinner setHidden:NO];
[spinner startAnimation:nil];
#endif
}
} requestCompleted:^(NSError * _Nullable error, UIImageLoaderImage * _Nullable image, UIImageLoadSource loadedFromSource) {
if(spinner) {
#if TARGET_OS_IOS || TARGET_OS_TV
[spinner setHidden:YES];
[spinner stopAnimating];
#elif TARGET_OS_OSX
[spinner setHidden:YES];
[spinner stopAnimation:nil];
#endif
}
if(image) {
//make sure the image completed matches the last requested image to display.
//this is most useful for table cells which are recycled.
NSURL * lastRequestedURL = objc_getAssociatedObject(self, _loadingURL);
if(lastRequestedURL == nil || [lastRequestedURL isEqual:request.URL]) {
NSNumber * completedImageScaling = objc_getAssociatedObject(self, _finalScaling);
if(completedImageScaling) {
#if TARGET_OS_IOS || TARGET_OS_TV
self.contentMode = completedImageScaling.integerValue;
#elif TARGET_OS_OSX
[self setImageScaling:completedImageScaling.integerValue];
#endif
}
self.image = image;
}
}
}];
objc_setAssociatedObject(self, _runningTask, task, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end