废话不说,直接上代码
封装.h
//// SignatureView.h// Copyright © 2019 dev. All rights reserved.//#import@interface SignatureView : UIView/** * 获取签名图片 */- (UIImage *)getSignatureImage;/** * 清除签名 */- (void)clearSignature;@end
封装.m
//// SignatureView.m// Copyright © 2019 dev. All rights reserved.//#import "SignatureView.h"@interface SignatureView (){ CGPoint points[5];}@property(nonatomic,assign) NSInteger control;@property(nonatomic,strong) UIBezierPath *beizerPath;@end@implementation SignatureView- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; [self setMultipleTouchEnabled:NO]; self.beizerPath = [UIBezierPath bezierPath]; [self.beizerPath setLineWidth:2]; } return self;}#pragma mark - 绘图操作- (void)drawRect:(CGRect)rect{ //设置签名的颜色 UIColor *strokeColor = [UIColor blackColor]; [strokeColor setStroke]; //签名的路径绘制 [self.beizerPath stroke];}- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{ self.control = 0; UITouch *touch = [touches anyObject]; points[0] = [touch locationInView:self]; CGPoint startPoint = points[0]; CGPoint endPoint = CGPointMake(startPoint.x + 1.5, startPoint.y + 2); [self.beizerPath moveToPoint:startPoint]; [self.beizerPath addLineToPoint:endPoint];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self]; _control++; points[_control] = touchPoint; if (_control == 4){ points[3] = CGPointMake((points[2].x + points[4].x)/2.0, (points[2].y + points[4].y)/2.0); //设置画笔起始点 [self.beizerPath moveToPoint:points[0]]; //endPoint终点 controlPoint1、controlPoint2控制点 [self.beizerPath addCurveToPoint:points[3] controlPoint1:points[1] controlPoint2:points[2]]; //setNeedsDisplay会自动调用drawRect方法,这样可以拿到UIGraphicsGetCurrentContext,就可以画画了 [self setNeedsDisplay]; points[0] = points[3]; points[1] = points[4]; _control = 1; }}#pragma mark - 清除签名- (void)clearSignature{ [self.beizerPath removeAllPoints]; [self setNeedsDisplay];}#pragma mark - 获取图片- (UIImage *)getSignatureImage { //设置为NO,UIView是透明这里的图片就是透明的 UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSString* docDir = [NSString stringWithFormat:@"%@/Documents/Image", NSHomeDirectory()]; [[NSFileManager defaultManager] createDirectoryAtPath:docDir withIntermediateDirectories:YES attributes:nil error:nil]; NSString *path = [NSString stringWithFormat:@"%@/Documents/Image/IMAGE.png", NSHomeDirectory()]; //用png是透明的 [UIImagePNGRepresentation(signatureImage) writeToFile: path atomically:YES];// [UIImageJPEGRepresentation(signatureImage,0.0001) writeToFile: path atomically:YES];//jpeg用这个return signatureImage;}@end
在vc里面使用
例如:
//// SignatureViewController.h// Copyright © 2019 dev. All rights reserved.//#import@interface SignatureViewController : UIViewController@property(nonatomic,strong)CommenModel *contentModel;@end
//// SignatureViewController.m// Copyright © 2019 dev. All rights reserved.//#import "SignatureViewController.h"#import "SignatureView.h"#define kScreenHeight [UIScreen mainScreen].bounds.size.height#define kScreenWidth [UIScreen mainScreen].bounds.size.width#define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]#define kBOUNDARY @"abc"@interface SignatureViewController ()@property(nonatomic,strong) SignatureView *signView;@property(nonatomic,strong) UIImageView *imageView;@end@implementation SignatureViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; [self addTopNaviBarWithTitle:@"签名"]; //画布 UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, 200)]; backView.layer.borderWidth = 2; backView.layer.borderColor = [[UIColor redColor] CGColor]; [self.view addSubview:backView]; self.signView = [[SignatureView alloc] initWithFrame:backView.bounds]; [backView addSubview:self.signView]; UIButton *reSignBtn = [UIButton buttonWithType:UIButtonTypeCustom]; reSignBtn.backgroundColor = NAVIBAR_COLOR_DEFAULT; [reSignBtn setTitle:@"重签" forState:UIControlStateNormal]; [reSignBtn setFrame:CGRectMake(20, SafeAreaTopHeight+200+10, (self.view.width - 20*3)*0.5, 40)]; reSignBtn.layer.cornerRadius = 5.0; reSignBtn.clipsToBounds = YES; reSignBtn.titleLabel.font = [UIFont systemFontOfSize:17]; [reSignBtn addTarget:self action:@selector(clearBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:reSignBtn]; UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [confirmBtn setTitle:@"确认" forState:UIControlStateNormal]; confirmBtn.backgroundColor = NAVIBAR_COLOR_DEFAULT; confirmBtn.titleLabel.font = [UIFont systemFontOfSize:17.0]; confirmBtn.frame = CGRectMake(CGRectGetMaxX(reSignBtn.frame)+20, reSignBtn.origin.y, reSignBtn.width, reSignBtn.height); confirmBtn.layer.cornerRadius = 5.0; confirmBtn.clipsToBounds = YES; [confirmBtn addTarget:self action:@selector(imageBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:confirmBtn]; }#pragma mark----导航栏-(void)addTopNaviBarWithTitle:(NSString *)title{ UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, SafeAreaTopHeight)]; UIColor *naviColor = NAVIBAR_COLOR_DEFAULT; naviBar.backgroundColor = naviColor; if(title.length) { UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH-40-40, 132.0 / SCALE)]; titleLabel.font = TITLE_FONT_SIZE; titleLabel.textColor = [UIColor whiteColor]; titleLabel.textAlignment = IFLY_ALIGN_CENTER; [titleLabel setCenter:CGPointMake(naviBar.center.x, naviBar.center.y + SCREEN_Y / 2.0)]; [naviBar addSubview:titleLabel]; titleLabel.hidden = NO; titleLabel.text = title; }else{ } UIButton *backBottomBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_Y, 60, 44)]; backBottomBtn.backgroundColor = [UIColor clearColor]; [backBottomBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; [naviBar addSubview:backBottomBtn]; UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(36.0 / SCALE,SCREEN_Y+ (44-66.0 / SCALE)/2, 66.0 / SCALE, 66.0 / SCALE)]; [backButton setImage:[UIImage imageNamed:@"Right"] forState:UIControlStateNormal]; [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; [naviBar addSubview:backButton]; [self.view addSubview:naviBar];}-(void)goBack{ [self.navigationController popViewControllerAnimated:YES];}#pragma mark - 清楚图片- (void)clearBtnClick{ [self.signView clearSignature];}#pragma mark - 生成图片- (void)imageBtnClick{ UIImage *image = [self.signView getSignatureImage]; //拿到图片做你想做的吧 /********************/}@end