-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathJMImageCacheOperation.m
More file actions
116 lines (88 loc) · 3.4 KB
/
JMImageCacheOperation.m
File metadata and controls
116 lines (88 loc) · 3.4 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
//
// JMImageCacheOperation.m
// JMCache
//
// Created by Rodrigo Garcia on 7/10/13.
//
//
#import "JMImageCacheOperation.h"
static inline NSString *JMImageCacheDirectory() {
static NSString *_JMImageCacheDirectory;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_JMImageCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/JMCache"] copy];
});
return _JMImageCacheDirectory;
}
inline static NSString *keyForURL(NSURL *url) {
return [url absoluteString];
}
static inline NSString *cachePathForKey(NSString *key) {
NSString *fileName = [NSString stringWithFormat:@"JMImageCache-%u", [key hash]];
return [JMImageCacheDirectory() stringByAppendingPathComponent:fileName];
}
@interface JMImageCacheOperation ()
@property(copy,nonatomic) NSURL *url;
@property(strong,nonatomic) UIImage *imageOperation;
@property(strong,nonatomic) NSData *dataRepresentation;
@end
@implementation JMImageCacheOperation
-(id)initWithURL:(NSURL *)aURL aSuccessBlock:(JMImageCacheSuccessBlock)aSuccessBlock andFailedBlock:(JMImageCacheFailedBlock)aFailedBlock
{
if(self=[super init])
{
self.url = aURL;
self.successBlock = aSuccessBlock;
self.failedBlock = aFailedBlock;
}
return self;
}
-(void)main
{
[self downloadImage];
NSString *cachePath = cachePathForKey(self.url.absoluteString);
[self writeData:self.dataRepresentation toPath:cachePath];
if (!self.isCancelled && self.imageOperation)
{
dispatch_async(dispatch_get_main_queue(), ^{
if(self.successBlock) self.successBlock(self.imageOperation);
});
}
}
- (void)downloadImage
{
NSURLRequest* request = [NSURLRequest requestWithURL:self.url];
NSURLResponse* response = nil;
NSError* error = nil;
self.dataRepresentation = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if(self.failedBlock) self.failedBlock(request, response, error);
});
return;
}
self.imageOperation = [[UIImage alloc] initWithData:self.dataRepresentation];
if (!self.imageOperation)
{
NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
[errorDetail setValue:[NSString stringWithFormat:@"Failed to init image with data from for URL: %@", self.url] forKey:NSLocalizedDescriptionKey];
NSError* error = [NSError errorWithDomain:@"JMImageCacheErrorDomain" code:1 userInfo:errorDetail];
dispatch_async(dispatch_get_main_queue(), ^{
if(self.failedBlock) self.failedBlock(request, response, error);
});
}
}
- (void)writeData:(NSData*)data toPath:(NSString *)path {
[data writeToFile:path atomically:YES];
NSString *cacheName = @"memory-cache";
NSDictionary *properties = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSNumber *actualCacheSize = [[NSUserDefaults standardUserDefaults] objectForKey:cacheName];
NSNumber *fileSize = [NSNumber numberWithUnsignedLong:([properties fileSize] + actualCacheSize.longValue)];
[[NSUserDefaults standardUserDefaults] setObject:fileSize forKey:cacheName];
}
-(BOOL)isConcurrent
{
return YES;
}
@end