-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUUImageCache.m
More file actions
149 lines (119 loc) · 3.26 KB
/
UUImageCache.m
File metadata and controls
149 lines (119 loc) · 3.26 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
//
// UUImageCache.m
// Useful Utilities - An easy to use UIImage cache
//
// License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: @cheesemaker or jon@threejacks.com
//
#import "UUImageCache.h"
#import "UUDataCache.h"
#import "UUDictionary.h"
#import "UURemoteData.h"
NSString * const kUUImageCacheMetaDataSizeKey = @"UUImageCacheMetaDataSize";
NSString * const kUUImageCacheMetaDataImageKey = @"UUImageCacheMetaDataImage";
@interface UUImageCache ()
@end
@implementation UUImageCache
+ (instancetype) sharedInstance
{
static id theSharedObject = nil;
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^
{
theSharedObject = [[[self class] alloc] init];
});
return theSharedObject;
}
- (id) init
{
self = [super init];
if (self)
{
self.remoteFetchEnabled = YES;
}
return self;
}
- (NSValue*) imageSizeForPath:(NSString*)path
{
NSValue* imageSize = nil;
NSDictionary* metaData = [[UURemoteData sharedInstance] metaDataForPath:path];
if (metaData)
{
imageSize = [metaData uuSafeGet:path forClass:[NSValue class]];
}
if (!imageSize)
{
// imageForPath will update the image size cache if needed
UIImage* img = [self imageForPath:path remoteDownloadIfNeeded:NO];
if (img)
{
imageSize = [NSValue valueWithCGSize:img.size];
}
}
return imageSize;
}
- (UIImage*) cachedImageForPath:(NSString*)path
{
UIImage* image = nil;
NSDictionary* metaData = [[UURemoteData sharedInstance] metaDataForPath:path];
if (metaData)
{
image = [metaData uuSafeGet:kUUImageCacheMetaDataImageKey forClass:[UIImage class]];
}
return image;
}
- (void) updateCache:(UIImage*)image forPath:(NSString*)path
{
NSMutableDictionary* md = nil;
NSDictionary* metaData = [[UURemoteData sharedInstance] metaDataForPath:path];
if (metaData)
{
md = [NSMutableDictionary dictionaryWithDictionary:metaData];
}
else
{
md = [NSMutableDictionary dictionary];
}
NSValue* imageSize = [NSValue valueWithCGSize:image.size];
[md setValue:imageSize forKey:kUUImageCacheMetaDataSizeKey];
[md setValue:image forKey:kUUImageCacheMetaDataImageKey];
[[UURemoteData sharedInstance] updateMetaData:md.copy forPath:path];
}
- (UIImage*) imageForPath:(NSString*)path
{
return [self imageForPath:path remoteDownloadIfNeeded:self.remoteFetchEnabled];
}
- (UIImage*) imageForPath:(NSString*)path remoteDownloadIfNeeded:(BOOL)remoteDownloadIfNeeded
{
if (!path || path.length <= 0)
{
return nil;
}
id obj = [self cachedImageForPath:path];
if (obj)
{
return obj;
}
NSData* data = nil;
if (remoteDownloadIfNeeded)
{
data = [[UURemoteData sharedInstance] dataForPath:path];
}
else
{
data = [[UUDataCache sharedCache] objectForKey:path];
}
if (data)
{
UIImage* img = [[UIImage alloc] initWithData:data];
if (img)
{
[self updateCache:img forPath:path];
}
return img;
}
return nil;
}
@end