-
Notifications
You must be signed in to change notification settings - Fork 20
/
NSImage+Resize.m
93 lines (81 loc) · 2.36 KB
/
NSImage+Resize.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
#import "NSImage+Resize.h"
@interface ImageCache : NSObject
+ (ImageCache *)instance;
- (void)setImage:(NSImage *)image
forName:(NSString *)name
withHeight:(CGFloat)height;
- (NSImage *)getImageForName:(NSString *)name withHeight:(CGFloat)height;
@property(nonatomic, strong) NSCache *imageCache;
@end
static ImageCache *instance;
@implementation ImageCache
+ (ImageCache *)instance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[ImageCache alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
self.imageCache = [[NSCache alloc] init];
}
return self;
}
- (NSString *)keyForName:(NSString *)name withHeight:(CGFloat)height {
return [NSString stringWithFormat:@"%f/%@", height, name];
}
- (void)setImage:(NSImage *)image
forName:(NSString *)name
withHeight:(CGFloat)height {
[self.imageCache setObject:image
forKey:[self keyForName:name withHeight:height]];
}
- (NSImage *)getImageForName:(NSString *)name withHeight:(CGFloat)height {
return
[self.imageCache objectForKey:[self keyForName:name withHeight:height]];
}
@end
@implementation NSImage (Resize)
- (NSImage *)imageWithHeight:(CGFloat)height {
NSImage *image = self;
if (![image isValid]) {
NSLog(@"Can't resize invalid image");
return nil;
}
NSSize newSize =
NSMakeSize(image.size.width * height / image.size.height, height);
NSImage *newImage = [[NSImage alloc] initWithSize:newSize];
[newImage lockFocus];
[image setSize:newSize];
[[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationDefault];
[image drawAtPoint:NSZeroPoint
fromRect:CGRectMake(0, 0, newSize.width, newSize.height)
operation:NSCompositingOperationCopy
fraction:1.0];
[newImage unlockFocus];
return newImage;
}
+ (NSImage *)imageFromName:(NSString *)name withHeight:(CGFloat)height {
if (name.length == 0) {
return nil;
}
NSImage *image =
[[ImageCache instance] getImageForName:name withHeight:height];
if (image != nil) {
return image;
}
if ([name hasPrefix:@"http"]) {
image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:name]];
} else {
image = [NSImage imageNamed:name];
}
if (height > 0 && image.size.height > height) {
image = [image imageWithHeight:height];
}
[[ImageCache instance] setImage:image forName:name withHeight:height];
return image;
}
@end