-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPLImage.m
More file actions
161 lines (119 loc) · 4.19 KB
/
PLImage.m
File metadata and controls
161 lines (119 loc) · 4.19 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
150
151
152
153
154
155
156
157
158
159
160
//
// PLImage.m
// PLImage
//
// Created by Lings on 12-7-24.
// Copyright (c) 2012年 Palmlife.me. All rights reserved.
//
#import "PLImage.h"
// Singleton Macro
#define PL_SINGLETON_GENERATOR(class_name, shared_func_name) \
static class_name * s_##class_name = nil; \
+ (class_name *)shared_func_name \
{ \
static dispatch_once_t once; \
dispatch_once(&once, ^{ \
s_##class_name = [[super allocWithZone:NULL] init]; \
}); \
return s_##class_name; \
} \
+ (class_name *)allocWithZone:(NSZone *)zone \
{ \
return s_##class_name; \
} \
- (class_name *)copyWithZone:(NSZone *)zone \
{ \
return self; \
} \
#define PL_SINGLETON_SHARED_INSTANCE(class_name) s_##class_name
#define PL_SINGLETON_CHECK_INITED(class_name) \
{if (PL_SINGLETON_SHARED_INSTANCE(class_name)) return PL_SINGLETON_SHARED_INSTANCE(class_name);}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@interface PLImageManager : NSObject
{
NSMutableDictionary * _imageWeakMap;
}
+ (PLImageManager *)sharedManager;
- (PLImage *)imageForKey:(NSString *)key;
- (void)setImage:(PLImage *)image forKey:(NSString *)key;
- (void)removeImageForKey:(NSString *)key;
- (void)removeAllImages;
@end
@implementation PLImageManager
PL_SINGLETON_GENERATOR(PLImageManager, sharedManager);
- (id)init
{
PL_SINGLETON_CHECK_INITED(PLImageManager)
if (self = [super init])
{
// create weak reference dictionary
CFDictionaryValueCallBacks valueCallBacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
_imageWeakMap = (__bridge NSMutableDictionary *)CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &valueCallBacks);
}
return self;
}
- (PLImage *)imageForKey:(NSString *)key
{
return [_imageWeakMap objectForKey:key];
}
- (void)setImage:(PLImage *)image forKey:(NSString *)key
{
if (!image) {
return;
}
[_imageWeakMap setObject:image forKey:key];
}
- (void)removeImageForKey:(NSString *)key
{
[_imageWeakMap removeObjectForKey:key];
}
- (void)removeAllImages
{
[_imageWeakMap removeAllObjects];
}
@end
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@implementation PLImage
@synthesize imageName = _imageName;
- (id)initWithName:(NSString *)name
{
NSString * imagePath = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
if (!imagePath) {
imagePath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"png"];
}
if (!imagePath) {
imagePath = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];
}
if (self = [super initWithContentsOfFile:imagePath]) {
_imageName = name;
}
return self;
}
+ (PLImage *)imageNamed:(NSString *)name
{
if (!name || !name.length) {
return nil;
}
// try to get from map
PLImage * cacheImg = [[PLImageManager sharedManager] imageForKey:name];
if (cacheImg) {
return cacheImg;
}
PLImage * image = [[self alloc] initWithName:name];
if (!image) {
return nil;
}
// add to map
[[PLImageManager sharedManager] setImage:image forKey:name];
return image;
}
- (void)dealloc
{
// remove from map
if (_imageName) {
[[PLImageManager sharedManager] removeImageForKey:_imageName];
}
}
@end