forked from twitter-archive/twui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TUIImage+Drawing.m
169 lines (149 loc) · 5.23 KB
/
TUIImage+Drawing.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
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
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "TUIKit.h"
#import "TUIImage+Drawing.h"
@implementation TUIImage (Drawing)
+ (TUIImage *)imageWithSize:(CGSize)size drawing:(void(^)(CGContextRef))draw
{
if(size.width < 1 || size.height < 1)
return nil;
CGContextRef ctx = TUICreateGraphicsContextWithOptions(size, NO);
draw(ctx);
TUIImage *i = TUIGraphicsContextGetImage(ctx);
CGContextRelease(ctx);
return i;
}
- (TUIImage *)scale:(CGSize)size
{
return [[self class] imageWithSize:size drawing:^(CGContextRef ctx) {
CGRect r;
r.origin = CGPointZero;
r.size = size;
CGContextDrawImage(ctx, r, self.CGImage);
}];
}
- (TUIImage *)crop:(CGRect)cropRect
{
if((cropRect.size.width < 1) || (cropRect.size.height < 1))
return nil;
CGSize s = self.size;
CGFloat mx = cropRect.origin.x + cropRect.size.width;
CGFloat my = cropRect.origin.y + cropRect.size.height;
if((cropRect.origin.x >= 0.0) && (cropRect.origin.y >= 0.0) && (mx <= s.width) && (my <= s.height)) {
// fast crop
CGImageRef cgimage = CGImageCreateWithImageInRect(self.CGImage, cropRect);
if(!cgimage) {
NSLog(@"CGImageCreateWithImageInRect failed %@ %@", NSStringFromRect(cropRect), NSStringFromSize(s));
return nil;
}
TUIImage *i = [[self class] imageWithCGImage:cgimage];
CGImageRelease(cgimage);
return i;
} else {
// slow crop - probably doing pad
return [[self class] imageWithSize:cropRect.size drawing:^(CGContextRef ctx) {
CGRect imageRect;
imageRect.origin.x = -cropRect.origin.x;
imageRect.origin.y = -cropRect.origin.y;
imageRect.size = s;
CGContextDrawImage(ctx, imageRect, self.CGImage);
}];
}
}
- (TUIImage *)upsideDownCrop:(CGRect)cropRect
{
CGSize s = self.size;
cropRect.origin.y = s.height - (cropRect.origin.y + cropRect.size.height);
return [self crop:cropRect];
}
- (TUIImage *)thumbnail:(CGSize)newSize
{
CGSize s = self.size;
float oldProp = s.width / s.height;
float newProp = newSize.width / newSize.height;
CGRect cropRect;
if (oldProp > newProp) {
cropRect.size.height = s.height;
cropRect.size.width = s.height * newProp;
} else {
cropRect.size.width = s.width;
cropRect.size.height = s.width / newProp;
}
cropRect.origin = CGPointMake((s.width - cropRect.size.width) / 2.0, (s.height - cropRect.size.height) / 2.0);
return [[self crop:cropRect] scale:newSize];
}
- (TUIImage *)pad:(CGFloat)padding
{
CGSize s = self.size;
return [self crop:CGRectMake(-padding, -padding, s.width + padding*2, s.height + padding*2)];
}
- (TUIImage *)roundImage:(CGFloat)radius
{
CGRect r;
r.origin = CGPointZero;
r.size = self.size;
return [[self class] imageWithSize:r.size drawing:^(CGContextRef ctx) {
CGContextClipToRoundRect(ctx, r, radius);
CGContextDrawImage(ctx, r, self.CGImage);
}];
}
- (TUIImage *)invertedMask
{
CGSize s = self.size;
return [[self class] imageWithSize:s drawing:^(CGContextRef ctx) {
CGRect rect = CGRectMake(0, 0, s.width, s.height);
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
CGContextFillRect(ctx, rect);
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, rect, self.CGImage);
CGContextClearRect(ctx, rect);
CGContextRestoreGState(ctx);
}];
}
- (TUIImage *)innerShadowWithOffset:(CGSize)offset radius:(CGFloat)radius color:(TUIColor *)color backgroundColor:(TUIColor *)backgroundColor
{
CGFloat padding = ceil(radius);
TUIImage *paddedImage = [self pad:padding];
TUIImage *shadowImage = [[self class] imageWithSize:paddedImage.size drawing:^(CGContextRef ctx) {
CGContextSaveGState(ctx);
CGRect r = CGRectMake(0, 0, paddedImage.size.width, paddedImage.size.height);
CGContextClipToMask(ctx, r, paddedImage.CGImage); // clip to image
CGContextSetShadowWithColor(ctx, offset, radius, color.CGColor);
CGContextBeginTransparencyLayer(ctx, NULL);
{
CGContextClipToMask(ctx, r, [[paddedImage invertedMask] CGImage]); // clip to inverted
CGContextSetFillColorWithColor(ctx, backgroundColor.CGColor);
CGContextFillRect(ctx, r); // draw with shadow
}
CGContextEndTransparencyLayer(ctx);
CGContextRestoreGState(ctx);
}];
return [shadowImage pad:-padding];
}
- (TUIImage *)embossMaskWithOffset:(CGSize)offset
{
CGFloat padding = MAX(offset.width, offset.height) + 1;
TUIImage *paddedImage = [self pad:padding];
CGSize s = paddedImage.size;
TUIImage *embossedImage = [[self class] imageWithSize:s drawing:^(CGContextRef ctx) {
CGContextSaveGState(ctx);
CGRect r = CGRectMake(0, 0, s.width, s.height);
CGContextClipToMask(ctx, r, [paddedImage CGImage]);
CGContextClipToMask(ctx, CGRectOffset(r, offset.width, offset.height), [[paddedImage invertedMask] CGImage]);
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
CGContextFillRect(ctx, r);
CGContextRestoreGState(ctx);
}];
return [embossedImage pad:-padding];
}
@end