废话不说,直接上代码
封装.h
//// SignatureVIEw.h// copyright © 2019 dev. All rights reserved.//#import <UIKit/UIKit.h>@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<UItouch *> *)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 <UIKit/UIKit.h>@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
以上是内存溢出为你收集整理的iOS手写签批全部内容,希望文章能够帮你解决iOS手写签批所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)