-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUIImage+ASConvenience.mm
234 lines (202 loc) · 10.9 KB
/
UIImage+ASConvenience.mm
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// UIImage+ASConvenience.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/UIImage+ASConvenience.h>
#import <AsyncDisplayKit/ASGraphicsContext.h>
#pragma mark - ASDKFastImageNamed
@implementation UIImage (ASDKFastImageNamed)
UIImage *cachedImageNamed(NSString *imageName, UITraitCollection *traitCollection) NS_RETURNS_RETAINED
{
static NSCache *imageCache = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Because NSCache responds to memory warnings, we do not need an explicit limit.
// all of these objects contain compressed image data and are relatively small
// compared to the backing stores of text and image views.
imageCache = [[NSCache alloc] init];
});
UIImage *image = nil;
if ([imageName length] > 0) {
NSString *imageKey = imageName;
if (traitCollection) {
char imageKeyBuffer[256];
if (@available(iOS 12.0, tvOS 10.0, *)) {
snprintf(imageKeyBuffer, sizeof(imageKeyBuffer), "%s|%ld|%ld|%ld", imageName.UTF8String, (long)traitCollection.horizontalSizeClass, (long)traitCollection.verticalSizeClass, (long)traitCollection.userInterfaceStyle);
} else {
// Fallback on earlier versions
snprintf(imageKeyBuffer, sizeof(imageKeyBuffer), "%s|%ld|%ld", imageName.UTF8String, (long)traitCollection.horizontalSizeClass, (long)traitCollection.verticalSizeClass);
}
imageKey = [NSString stringWithUTF8String:imageKeyBuffer];
}
image = [imageCache objectForKey:imageKey];
if (!image) {
image = [UIImage imageNamed:imageName inBundle:nil compatibleWithTraitCollection:traitCollection];
if (image) {
[imageCache setObject:image forKey:imageKey];
}
}
}
return image;
}
+ (UIImage *)as_imageNamed:(NSString *)imageName NS_RETURNS_RETAINED
{
return cachedImageNamed(imageName, nil);
}
+ (UIImage *)as_imageNamed:(NSString *)imageName compatibleWithTraitCollection:(UITraitCollection *)traitCollection NS_RETURNS_RETAINED
{
return cachedImageNamed(imageName, traitCollection);
}
@end
#pragma mark - ASDKResizableRoundedRects
@implementation UIImage (ASDKResizableRoundedRects)
+ (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
cornerColor:(UIColor *)cornerColor
fillColor:(UIColor *)fillColor NS_RETURNS_RETAINED
{
return [self as_resizableRoundedImageWithCornerRadius:cornerRadius
cornerColor:cornerColor
fillColor:fillColor
borderColor:nil
borderWidth:1.0
roundedCorners:UIRectCornerAllCorners
scale:0.0];
}
+ (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
cornerColor:(UIColor *)cornerColor
fillColor:(UIColor *)fillColor
traitCollection:(ASPrimitiveTraitCollection) traitCollection NS_RETURNS_RETAINED
{
return [self as_resizableRoundedImageWithCornerRadius:cornerRadius
cornerColor:cornerColor
fillColor:fillColor
borderColor:nil
borderWidth:1.0
roundedCorners:UIRectCornerAllCorners
scale:0.0
traitCollection:traitCollection];
}
+ (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
cornerColor:(UIColor *)cornerColor
fillColor:(UIColor *)fillColor
borderColor:(UIColor *)borderColor
borderWidth:(CGFloat)borderWidth
traitCollection:(ASPrimitiveTraitCollection) traitCollection NS_RETURNS_RETAINED {
return [self as_resizableRoundedImageWithCornerRadius:cornerRadius
cornerColor:cornerColor
fillColor:fillColor
borderColor:borderColor
borderWidth:borderWidth
roundedCorners:UIRectCornerAllCorners
scale:0.0
traitCollection:traitCollection];
}
+ (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
cornerColor:(UIColor *)cornerColor
fillColor:(UIColor *)fillColor
borderColor:(UIColor *)borderColor
borderWidth:(CGFloat)borderWidth NS_RETURNS_RETAINED
{
return [self as_resizableRoundedImageWithCornerRadius:cornerRadius
cornerColor:cornerColor
fillColor:fillColor
borderColor:borderColor
borderWidth:borderWidth
roundedCorners:UIRectCornerAllCorners
scale:0.0];
}
+ (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
cornerColor:(UIColor *)cornerColor
fillColor:(UIColor *)fillColor
borderColor:(UIColor *)borderColor
borderWidth:(CGFloat)borderWidth
roundedCorners:(UIRectCorner)roundedCorners
scale:(CGFloat)scale NS_RETURNS_RETAINED {
return [self as_resizableRoundedImageWithCornerRadius:cornerRadius
cornerColor:cornerColor
fillColor:fillColor
borderColor:borderColor
borderWidth:borderWidth
roundedCorners:roundedCorners
scale:scale
traitCollection:ASPrimitiveTraitCollectionMakeDefault()];
}
+ (UIImage *)as_resizableRoundedImageWithCornerRadius:(CGFloat)cornerRadius
cornerColor:(UIColor *)cornerColor
fillColor:(UIColor *)fillColor
borderColor:(UIColor *)borderColor
borderWidth:(CGFloat)borderWidth
roundedCorners:(UIRectCorner)roundedCorners
scale:(CGFloat)scale
traitCollection:(ASPrimitiveTraitCollection) traitCollection NS_RETURNS_RETAINED
{
static NSCache *__pathCache = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__pathCache = [[NSCache alloc] init];
// UIBezierPath objects are fairly small and these are equally sized. 20 should be plenty for many different parameters.
__pathCache.countLimit = 20;
});
// Treat clear background color as no background color
if ([cornerColor isEqual:[UIColor clearColor]]) {
cornerColor = nil;
}
CGFloat dimension = (cornerRadius * 2) + 1;
CGRect bounds = CGRectMake(0, 0, dimension, dimension);
typedef struct {
UIRectCorner corners;
CGFloat radius;
} PathKey;
PathKey key = { roundedCorners, cornerRadius };
NSValue *pathKeyObject = [[NSValue alloc] initWithBytes:&key objCType:@encode(PathKey)];
CGSize cornerRadii = CGSizeMake(cornerRadius, cornerRadius);
UIBezierPath *path = [__pathCache objectForKey:pathKeyObject];
if (path == nil) {
path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:roundedCorners cornerRadii:cornerRadii];
[__pathCache setObject:path forKey:pathKeyObject];
}
// We should probably check if the background color has any alpha component but that
// might be expensive due to needing to check mulitple color spaces.
UIImage *result = ASGraphicsCreateImage(traitCollection, bounds.size, cornerColor != nil, scale, nil, nil, ^{
BOOL contextIsClean = YES;
if (cornerColor) {
contextIsClean = NO;
[cornerColor setFill];
// Copy "blend" mode is extra fast because it disregards any value currently in the buffer and overrides directly.
UIRectFillUsingBlendMode(bounds, kCGBlendModeCopy);
}
BOOL canUseCopy = contextIsClean || (CGColorGetAlpha(fillColor.CGColor) == 1);
[fillColor setFill];
[path fillWithBlendMode:(canUseCopy ? kCGBlendModeCopy : kCGBlendModeNormal) alpha:1];
if (borderColor) {
[borderColor setStroke];
// Inset border fully inside filled path (not halfway on each side of path)
CGRect strokeRect = CGRectInset(bounds, borderWidth / 2.0, borderWidth / 2.0);
// It is rarer to have a stroke path, and our cache key only handles rounded rects for the exact-stretchable
// size calculated by cornerRadius, so we won't bother caching this path. Profiling validates this decision.
UIBezierPath *strokePath = [UIBezierPath bezierPathWithRoundedRect:strokeRect
byRoundingCorners:roundedCorners
cornerRadii:cornerRadii];
[strokePath setLineWidth:borderWidth];
BOOL canUseCopy = (CGColorGetAlpha(borderColor.CGColor) == 1);
[strokePath strokeWithBlendMode:(canUseCopy ? kCGBlendModeCopy : kCGBlendModeNormal) alpha:1];
}
// Refill the center area with fillColor since it may be contaminated by the sub pixel
// rendering.
if (borderWidth > 0) {
CGRect rect = CGRectMake(capInset, capInset, 1, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [fillColor CGColor]);
CGContextFillRect(context, rect);
}
});
UIEdgeInsets capInsets = UIEdgeInsetsMake(cornerRadius, cornerRadius, cornerRadius, cornerRadius);
result = [result resizableImageWithCapInsets:capInsets resizingMode:UIImageResizingModeStretch];
return result;
}
@end