iOS - 手写签名(透明背景)
2020-08-06 本文已影响0人
Joh蜗牛
1.引入【#import "BJTSignView.h"】
代码如下:
#import <UIKit/UIKit.h>
@interface BJTSignView : UIView
/**
* 获取签名图片
*/
- (UIImage *)getSignatureImage;
/**
* 清除签名
*/
- (void)clearSignature;
@end
#import "BJTSignView.h"
@interface BJTSignView (){
CGPoint points[5];
}
@property(nonatomic,assign) NSInteger control;
@property(nonatomic,strong) UIBezierPath *beizerPath;
@end
@implementation BJTSignView
- (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 redColor];
[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];
return signatureImage;
}
@end
2.实现方法:
定义属性:
@property(nonatomic,strong) BJTSignView *signView;
@property(nonatomic,strong) UIImageView *imageView;
实现代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//画布
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, kScreenWidth, 200)];
backView.layer.borderWidth = 2;
backView.layer.borderColor = [[UIColor redColor] CGColor];
[self.view addSubview:backView];
self.signView = [[BJTSignView alloc] initWithFrame:backView.bounds];
[backView addSubview:self.signView];
UIButton *clearBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 260, kScreenWidth, 30)];
[clearBtn setTitle:@"清除签名" forState:UIControlStateNormal];
[clearBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:clearBtn];
[clearBtn addTarget:self action:@selector(clearBtnClick) forControlEvents:UIControlEventTouchUpInside];
UIButton *imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 300, kScreenWidth, 30)];
[imageBtn setTitle:@"生成图片" forState:UIControlStateNormal];
[imageBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:imageBtn];
[imageBtn addTarget:self action:@selector(imageBtnClick) forControlEvents:UIControlEventTouchUpInside];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - kScreenWidth/2)/2,380 , kScreenWidth/2, 200/2)];
self.imageView.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.imageView];
}
#pragma mark - 清楚图片
- (void)clearBtnClick{
[self.signView clearSignature];
}
#pragma mark - 生成图片
- (void)imageBtnClick{
UIImage *image = [self.signView getSignatureImage];
self.imageView.image = image;
}