-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSignatureView.mm
346 lines (261 loc) · 11.1 KB
/
SignatureView.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// SignatureView.m
// SignatureView
//
// Created by Michal Konturek on 05/05/2014.
// Copyright (c) 2014 Michal Konturek. All rights reserved.
//
#import "SignatureView.h"
@interface SignatureView ()
@property (nonatomic, strong) NSMutableArray *drawnPoints;
@property (nonatomic, assign) CGPoint previousPoint;
@property (nonatomic, strong) UIImage *tempImage;
@property (nonatomic, assign) BOOL blank;
@end
@implementation SignatureView
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self _initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self _initialize];
}
return self;
}
- (void)_initialize {
self.userInteractionEnabled = YES;
self.blank = YES;
[self _setupDefaultValues];
[self _initializeRecognizer];
}
- (void)_setupDefaultValues {
self.foregroundLineColor = [UIColor redColor];
self.foregroundLineWidth = 3.0;
self.backgroundLineColor = [UIColor blackColor];
self.backgroundLineWidth = 3.0;
}
- (void)_initializeRecognizer {
id recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(clear)];
self.recognizer = recognizer;
[self addGestureRecognizer:recognizer];
}
- (void)setLineColor:(UIColor *)color {
self.foregroundLineColor = color;
self.backgroundLineColor = color;
}
- (void)setLineWidth:(CGFloat)width {
self.backgroundLineWidth = width;
self.foregroundLineWidth = width;
}
- (void)clear {
self.blank = YES;
[self clearWithColor:[UIColor whiteColor]];
[self clearWithColor:[UIColor clearColor]];
}
- (void)clearWithColor:(UIColor *)color {
CGSize screenSize = self.frame.size;
UIGraphicsBeginImageContext(screenSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, screenSize.width, screenSize.height)];
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, CGRectMake(0, 0, screenSize.width, screenSize.height));
UIImage *cleanImage = UIGraphicsGetImageFromCurrentImageContext();
self.image = cleanImage;
UIGraphicsEndImageContext();
}
- (BOOL)isSigned {
return !self.blank;
}
- (UIImage *)signatureImage {
return [self.image copy];
}
- (NSData *)signatureData {
return UIImagePNGRepresentation(self.image);
}
#pragma mark - Touch handlers
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint currentPoint = [self _touchPointForTouches:touches];
self.drawnPoints = [NSMutableArray arrayWithObject:[NSValue valueWithCGPoint:currentPoint]];
self.previousPoint = currentPoint;
/*
To be able to replace the jagged polylines with the smooth
polylines, we need to save the unmodified image.
*/
self.tempImage = self.image;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint currentPoint = [self _touchPointForTouches:touches];
[self.drawnPoints addObject:[NSValue valueWithCGPoint:currentPoint]];
self.image = [self _drawLineFromPoint:self.previousPoint toPoint:currentPoint image:self.image];
self.previousPoint = currentPoint;
self.blank = NO;
}
- (CGPoint)_touchPointForTouches:(NSSet *)touches {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
return point;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *generalizedPoints = [self _douglasPeucker:self.drawnPoints epsilon:2];
NSArray *splinePoints = [self _catmullRomSpline:generalizedPoints segments:4];
self.image = [self _drawLineWithPoints:splinePoints image:self.tempImage];
self.drawnPoints = nil;
self.tempImage = nil;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
#pragma mark - Drawing
- (UIImage *)_drawLineFromPoint:(CGPoint)fromPoint
toPoint:(CGPoint)toPoint image:(UIImage *)image {
CGSize screenSize = self.frame.size;
if (UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(screenSize, NO, 0.0);
} else {
UIGraphicsBeginImageContext(screenSize);
}
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0, 0, screenSize.width, screenSize.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.foregroundLineWidth);
CGContextSetStrokeColorWithColor(context, self.foregroundLineColor.CGColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
- (UIImage *)_drawLineWithPoints:(NSArray *)points image:(UIImage *)image {
CGSize screenSize = self.frame.size;
if (UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(screenSize, NO, 0.0);
} else {
UIGraphicsBeginImageContext(screenSize);
}
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0, 0, screenSize.width, screenSize.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.backgroundLineWidth);
CGContextSetStrokeColorWithColor(context, self.backgroundLineColor.CGColor);
CGContextBeginPath(context);
NSInteger count = [points count];
CGPoint point = [[points objectAtIndex:0] CGPointValue];
CGContextMoveToPoint(context, point.x, point.y);
for(int i = 1; i < count; i++) {
point = [[points objectAtIndex:i] CGPointValue];
CGContextAddLineToPoint(context, point.x, point.y);
}
CGContextStrokePath(context);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
#pragma mark - Smoothing Alrgorithms
- (NSArray *)_douglasPeucker:(NSArray *)points epsilon:(float)epsilon {
NSInteger count = [points count];
if(count < 3) return points;
//Find the point with the maximum distance
float dmax = 0;
int index = 0;
for(int i = 1; i < count - 1; i++) {
CGPoint point = [[points objectAtIndex:i] CGPointValue];
CGPoint lineA = [[points objectAtIndex:0] CGPointValue];
CGPoint lineB = [[points objectAtIndex:count - 1] CGPointValue];
float d = [self _perpendicularDistance:point lineA:lineA lineB:lineB];
if(d > dmax) {
index = i;
dmax = d;
}
}
// If max distance is greater than epsilon, recursively simplify
NSArray *resultList;
if(dmax > epsilon) {
NSArray *recResults1 = [self _douglasPeucker:[points subarrayWithRange:NSMakeRange(0, index + 1)]
epsilon:epsilon];
NSArray *recResults2 = [self _douglasPeucker:[points subarrayWithRange:NSMakeRange(index, count - index)]
epsilon:epsilon];
NSMutableArray *tmpList = [NSMutableArray arrayWithArray:recResults1];
[tmpList removeLastObject];
[tmpList addObjectsFromArray:recResults2];
resultList = tmpList;
} else {
resultList = [NSArray arrayWithObjects:[points objectAtIndex:0], [points objectAtIndex:count - 1],nil];
}
return resultList;
}
- (float)_perpendicularDistance:(CGPoint)point
lineA:(CGPoint)lineA lineB:(CGPoint)lineB {
CGPoint v1 = CGPointMake(lineB.x - lineA.x, lineB.y - lineA.y);
CGPoint v2 = CGPointMake(point.x - lineA.x, point.y - lineA.y);
float lenV1 = sqrt(v1.x * v1.x + v1.y * v1.y);
float lenV2 = sqrt(v2.x * v2.x + v2.y * v2.y);
float angle = acos((v1.x * v2.x + v1.y * v2.y) / (lenV1 * lenV2));
return sin(angle) * lenV2;
}
- (NSArray *)_catmullRomSpline:(NSArray *)points segments:(int)segments {
NSInteger count = [points count];
if(count < 4) return points;
float b[segments][4];
{
// precompute interpolation parameters
float t = 0.0f;
float dt = 1.0f/(float)segments;
for (int i = 0; i < segments; i++, t+=dt) {
float tt = t*t;
float ttt = tt * t;
b[i][0] = 0.5f * (-ttt + 2.0f*tt - t);
b[i][1] = 0.5f * (3.0f*ttt -5.0f*tt +2.0f);
b[i][2] = 0.5f * (-3.0f*ttt + 4.0f*tt + t);
b[i][3] = 0.5f * (ttt - tt);
}
}
NSMutableArray *resultArray = [NSMutableArray array];
{
int i = 0; // first control point
[resultArray addObject:[points objectAtIndex:0]];
for (int j = 1; j < segments; j++) {
CGPoint pointI = [[points objectAtIndex:i] CGPointValue];
CGPoint pointIp1 = [[points objectAtIndex:(i + 1)] CGPointValue];
CGPoint pointIp2 = [[points objectAtIndex:(i + 2)] CGPointValue];
float px = (b[j][0]+b[j][1])*pointI.x + b[j][2]*pointIp1.x + b[j][3]*pointIp2.x;
float py = (b[j][0]+b[j][1])*pointI.y + b[j][2]*pointIp1.y + b[j][3]*pointIp2.y;
[resultArray addObject:[NSValue valueWithCGPoint:CGPointMake(px, py)]];
}
}
for (int i = 1; i < count - 2; i++) {
// the first interpolated point is always the original control point
[resultArray addObject:[points objectAtIndex:i]];
for (int j = 1; j < segments; j++) {
CGPoint pointIm1 = [[points objectAtIndex:(i - 1)] CGPointValue];
CGPoint pointI = [[points objectAtIndex:i] CGPointValue];
CGPoint pointIp1 = [[points objectAtIndex:(i + 1)] CGPointValue];
CGPoint pointIp2 = [[points objectAtIndex:(i + 2)] CGPointValue];
float px = b[j][0]*pointIm1.x + b[j][1]*pointI.x + b[j][2]*pointIp1.x + b[j][3]*pointIp2.x;
float py = b[j][0]*pointIm1.y + b[j][1]*pointI.y + b[j][2]*pointIp1.y + b[j][3]*pointIp2.y;
[resultArray addObject:[NSValue valueWithCGPoint:CGPointMake(px, py)]];
}
}
{
NSInteger i = count - 2; // second to last control point
[resultArray addObject:[points objectAtIndex:i]];
for (int j = 1; j < segments; j++) {
CGPoint pointIm1 = [[points objectAtIndex:(i - 1)] CGPointValue];
CGPoint pointI = [[points objectAtIndex:i] CGPointValue];
CGPoint pointIp1 = [[points objectAtIndex:(i + 1)] CGPointValue];
float px = b[j][0]*pointIm1.x + b[j][1]*pointI.x + (b[j][2]+b[j][3])*pointIp1.x;
float py = b[j][0]*pointIm1.y + b[j][1]*pointI.y + (b[j][2]+b[j][3])*pointIp1.y;
[resultArray addObject:[NSValue valueWithCGPoint:CGPointMake(px, py)]];
}
}
// the very last interpolated point is the last control point
[resultArray addObject:[points objectAtIndex:(count - 1)]];
return resultArray;
}
@end