From 6215efb014e521d213cdc3c5f863bc342f09c52f Mon Sep 17 00:00:00 2001 From: Peter Staev Date: Thu, 25 May 2017 12:41:02 +0300 Subject: [PATCH 1/3] adds support get signature as svg --- Source/SignatureView.h | 1 + Source/SignatureView.m | 48 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Source/SignatureView.h b/Source/SignatureView.h index da7851c..f6b869b 100644 --- a/Source/SignatureView.h +++ b/Source/SignatureView.h @@ -26,6 +26,7 @@ - (UIImage *)signatureImage; - (NSData *)signatureData; +- (NSString *)signatureSvg; - (BOOL)isSigned; diff --git a/Source/SignatureView.m b/Source/SignatureView.m index 7a29574..a9097eb 100644 --- a/Source/SignatureView.m +++ b/Source/SignatureView.m @@ -16,6 +16,8 @@ @interface SignatureView () @property (nonatomic, assign) BOOL blank; +@property (nonatomic, retain) NSMutableString *svgPath; + @end @implementation SignatureView @@ -40,6 +42,7 @@ - (void)_initialize { self.userInteractionEnabled = YES; self.blank = YES; + self.svgPath = [[NSMutableString alloc] init]; [self _setupDefaultValues]; [self _initializeRecognizer]; @@ -72,7 +75,8 @@ - (void)setLineWidth:(CGFloat)width { - (void)clear { self.blank = YES; - + [self.svgPath setString:@""]; + [self clearWithColor:[UIColor whiteColor]]; [self clearWithColor:[UIColor clearColor]]; } @@ -106,6 +110,46 @@ - (NSData *)signatureData { return UIImagePNGRepresentation(self.image); } +- (NSString *)signatureSvg { + const unsigned width = (unsigned)self.bounds.size.width; + const unsigned height = (unsigned)self.bounds.size.height; + const unsigned strokeWidth = (unsigned)self.backgroundLineWidth; + + const CGColorSpaceModel colorSpace = CGColorSpaceGetModel(CGColorGetColorSpace(self.backgroundLineColor.CGColor)); + const CGFloat *colorComponents = CGColorGetComponents(self.backgroundLineColor.CGColor); + int r = 0; + int g = 0; + int b = 0; + int a = 255; + + if (colorSpace == kCGColorSpaceModelMonochrome) { + r = (int)(colorComponents[0] * 255); + g = (int)(colorComponents[0] * 255); + b = (int)(colorComponents[0] * 255); + a = (int)(colorComponents[1] * 255); + } + else if (colorSpace == kCGColorSpaceModelRGB) { + r = (int)(colorComponents[0] * 255); + g = (int)(colorComponents[1] * 255); + b = (int)(colorComponents[2] * 255); + a = (int)(colorComponents[3] * 255); + } + + NSString* svgAsString = [NSString stringWithFormat:@"\n" + @"\n" + @" \n" + @"", + width, + height, + height, + width, + r, g, b, a, + strokeWidth, + self.svgPath + ]; + return svgAsString; +} + #pragma mark - Touch handlers @@ -208,9 +252,11 @@ - (UIImage *)_drawLineWithPoints:(NSArray *)points image:(UIImage *)image { NSInteger count = [points count]; CGPoint point = [[points objectAtIndex:0] CGPointValue]; CGContextMoveToPoint(context, point.x, point.y); + [self.svgPath appendFormat:@"M%.1lf %.1lf", point.x, point.y]; for(int i = 1; i < count; i++) { point = [[points objectAtIndex:i] CGPointValue]; CGContextAddLineToPoint(context, point.x, point.y); + [self.svgPath appendFormat:@"L%.1lf %.1lf", point.x, point.y]; } CGContextStrokePath(context); From ea5ada1fa9d18882c935b18f5eda67ec6e180545 Mon Sep 17 00:00:00 2001 From: Peter Staev Date: Thu, 25 May 2017 14:31:13 +0300 Subject: [PATCH 2/3] correctly clear signature for both clear methods --- Source/SignatureView.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/SignatureView.m b/Source/SignatureView.m index a9097eb..63d042e 100644 --- a/Source/SignatureView.m +++ b/Source/SignatureView.m @@ -74,14 +74,14 @@ - (void)setLineWidth:(CGFloat)width { } - (void)clear { - self.blank = YES; - [self.svgPath setString:@""]; - [self clearWithColor:[UIColor whiteColor]]; [self clearWithColor:[UIColor clearColor]]; } - (void)clearWithColor:(UIColor *)color { + self.blank = YES; + [self.svgPath setString:@""]; + CGSize screenSize = self.frame.size; UIGraphicsBeginImageContext(screenSize); From be91acb66a6401315fdf4ed5e941ab5c46b9c353 Mon Sep 17 00:00:00 2001 From: Peter Staev Date: Tue, 13 Jun 2017 12:22:27 +0300 Subject: [PATCH 3/3] remove alpha component from stroke and better svg path format --- Source/SignatureView.m | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Source/SignatureView.m b/Source/SignatureView.m index 63d042e..c343d80 100644 --- a/Source/SignatureView.m +++ b/Source/SignatureView.m @@ -120,32 +120,29 @@ - (NSString *)signatureSvg { int r = 0; int g = 0; int b = 0; - int a = 255; if (colorSpace == kCGColorSpaceModelMonochrome) { r = (int)(colorComponents[0] * 255); g = (int)(colorComponents[0] * 255); b = (int)(colorComponents[0] * 255); - a = (int)(colorComponents[1] * 255); } else if (colorSpace == kCGColorSpaceModelRGB) { r = (int)(colorComponents[0] * 255); g = (int)(colorComponents[1] * 255); b = (int)(colorComponents[2] * 255); - a = (int)(colorComponents[3] * 255); } NSString* svgAsString = [NSString stringWithFormat:@"\n" @"\n" - @" \n" + @" \n" @"", width, height, height, width, - r, g, b, a, + r, g, b, strokeWidth, - self.svgPath + [self.svgPath stringByTrimmingCharactersInSet: NSCharacterSet.whitespaceCharacterSet] ]; return svgAsString; } @@ -252,11 +249,11 @@ - (UIImage *)_drawLineWithPoints:(NSArray *)points image:(UIImage *)image { NSInteger count = [points count]; CGPoint point = [[points objectAtIndex:0] CGPointValue]; CGContextMoveToPoint(context, point.x, point.y); - [self.svgPath appendFormat:@"M%.1lf %.1lf", point.x, point.y]; + [self.svgPath appendFormat:@" M%.1lf %.1lf", point.x, point.y]; for(int i = 1; i < count; i++) { point = [[points objectAtIndex:i] CGPointValue]; CGContextAddLineToPoint(context, point.x, point.y); - [self.svgPath appendFormat:@"L%.1lf %.1lf", point.x, point.y]; + [self.svgPath appendFormat:@" L%.1lf %.1lf", point.x, point.y]; } CGContextStrokePath(context);