-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSGImageCacheTask.m
51 lines (42 loc) · 1.25 KB
/
SGImageCacheTask.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
//
// Created by matt on 22/05/14.
//
#import "SGImageCacheTask.h"
#import "SGCacheTaskPrivate.h"
#import "SGCachePrivate.h"
#import "SGImageCache.h"
#import "SGImageCachePrivate.h"
@implementation SGImageCacheTask
- (id)init {
self = [super init];
self.cacheClass = SGImageCache.class;
return self;
}
- (void)completedWithFile:(NSData *)data {
UIImage *image = [UIImage imageWithData:data];
if (image) {
if ([SGImageCache imageFromMemCacheForCacheKey:self.cacheKey]) {
[SGImageCache setImageInMemCache:image forCacheKey:self.cacheKey];
}
[SGImageCache addData:data forCacheKey:self.cacheKey];
} else {
[self finish];
return;
}
// force a decompress
if (self.forceDecompress) {
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), image.CGImage);
UIGraphicsEndImageContext();
}
// call the completion blocks on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
for (SGCacheFetchCompletion completion in self.completions) {
completion(image);
}
});
self.succeeded = YES;
[self finish];
}
@end