-
Notifications
You must be signed in to change notification settings - Fork 5
/
PSWResources.m
84 lines (78 loc) · 2.69 KB
/
PSWResources.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
#import "PSWResources.h"
#import <CoreGraphics/CoreGraphics.h>
static NSMutableDictionary *imageCache;
UIImage *PSWGetCachedImageResource(NSString *name, NSBundle *bundle)
{
NSString *key = [NSString stringWithFormat:@"%@#%@", [bundle bundlePath], name];
UIImage *result = [imageCache objectForKey:key];
if (!result) {
if (!imageCache)
imageCache = [[NSMutableDictionary alloc] init];
result = [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:@"png"]];
if (result)
if (imageCache)
[imageCache setObject:result forKey:key];
else
imageCache = [[NSMutableDictionary alloc] initWithObjectsAndKeys:result, key, nil];
}
return result;
}
UIImage *PSWImage(NSString *name)
{
NSString *key = [NSString stringWithFormat:@"/Applications/ProSwitcher.app/%@.png", name];
UIImage *result = [imageCache objectForKey:key];
if (!result) {
if (!imageCache)
imageCache = [[NSMutableDictionary alloc] init];
result = [UIImage imageWithContentsOfFile:key];
if (result)
if (imageCache)
[imageCache setObject:result forKey:key];
else
imageCache = [[NSMutableDictionary alloc] initWithObjectsAndKeys:result, key, nil];
}
return result;
}
static void ClipContextRounded(CGContextRef c, CGSize size, CGFloat cornerRadius)
{
CGSize half;
half.width = size.width / 2.0f;
half.height = size.height / 2.0f;
CGContextMoveToPoint(c, size.width, half.height);
CGContextAddArcToPoint(c, size.width, size.height, half.width, size.height, cornerRadius);
CGContextAddArcToPoint(c, 0.0f, size.height, 0.0f, half.height, cornerRadius);
CGContextAddArcToPoint(c, 0.0f, 0.0f, half.width, 0.0f, cornerRadius);
CGContextAddArcToPoint(c, size.width, 0.0f, size.width, half.height, cornerRadius);
CGContextClosePath(c);
CGContextClip(c);
}
UIImage *PSWGetCachedCornerMaskOfSize(CGSize size, CGFloat cornerRadius)
{
if (size.width < 1.0f || size.height < 1.0f)
return nil;
NSString *key = [NSString stringWithFormat:@"%fx%f-%f", size.width, size.height, cornerRadius];
UIImage *result = [imageCache objectForKey:key];
if (!result) {
UIGraphicsBeginImageContext(size);
CGContextRef c = UIGraphicsGetCurrentContext();
// TODO: figure out why sometimes this fails so hard
if (!c)
return nil;
if (cornerRadius != 0.0f)
ClipContextRounded(c, size, cornerRadius);
[[UIColor whiteColor] set];
UIRectFill(CGRectMake(0.0f, 0.0f, size.width, size.height));
result = UIGraphicsGetImageFromCurrentImageContext();
if (imageCache)
[imageCache setObject:result forKey:key];
else
imageCache = [[NSMutableDictionary alloc] initWithObjectsAndKeys:result, key, nil];
UIGraphicsEndImageContext();
}
return result;
}
void PSWClearResourceCache()
{
[imageCache release];
imageCache = nil;
}