BJTSignView.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // BJTSignView.m
  3. // BJTResearch
  4. //
  5. // Created by yunlong on 2017/6/28.
  6. // Copyright © 2017年 yunlong. All rights reserved.
  7. //
  8. #import "BJTSignView.h"
  9. @interface BJTSignView (){
  10. CGPoint points[5];
  11. }
  12. @property(nonatomic,assign) NSInteger control;
  13. @property(nonatomic,strong) UIBezierPath *beizerPath;
  14. @end
  15. @implementation BJTSignView
  16. - (instancetype)initWithFrame:(CGRect)frame
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. self.backgroundColor = [UIColor clearColor];
  21. [self setMultipleTouchEnabled:NO];
  22. self.beizerPath = [UIBezierPath bezierPath];
  23. [self.beizerPath setLineWidth:2];
  24. }
  25. return self;
  26. }
  27. #pragma mark - 绘图操作
  28. - (void)drawRect:(CGRect)rect{
  29. //设置签名的颜色
  30. UIColor *strokeColor = [UIColor redColor];
  31. [strokeColor setStroke];
  32. //签名的路径绘制
  33. [self.beizerPath stroke];
  34. }
  35. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  36. self.control = 0;
  37. UITouch *touch = [touches anyObject];
  38. points[0] = [touch locationInView:self];
  39. CGPoint startPoint = points[0];
  40. CGPoint endPoint = CGPointMake(startPoint.x + 1.5, startPoint.y + 2);
  41. [self.beizerPath moveToPoint:startPoint];
  42. [self.beizerPath addLineToPoint:endPoint];
  43. }
  44. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
  45. UITouch *touch = [touches anyObject];
  46. CGPoint touchPoint = [touch locationInView:self];
  47. _control++;
  48. points[_control] = touchPoint;
  49. if (_control == 4){
  50. points[3] = CGPointMake((points[2].x + points[4].x)/2.0, (points[2].y + points[4].y)/2.0);
  51. //设置画笔起始点
  52. [self.beizerPath moveToPoint:points[0]];
  53. //endPoint终点 controlPoint1、controlPoint2控制点
  54. [self.beizerPath addCurveToPoint:points[3] controlPoint1:points[1] controlPoint2:points[2]];
  55. //setNeedsDisplay会自动调用drawRect方法,这样可以拿到UIGraphicsGetCurrentContext,就可以画画了
  56. [self setNeedsDisplay];
  57. points[0] = points[3];
  58. points[1] = points[4];
  59. _control = 1;
  60. }
  61. }
  62. #pragma mark - 清除签名
  63. - (void)clearSignature{
  64. [self.beizerPath removeAllPoints];
  65. [self setNeedsDisplay];
  66. }
  67. #pragma mark - 获取图片
  68. - (UIImage *)getSignatureImage {
  69. //设置为NO,UIView是透明这里的图片就是透明的
  70. UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
  71. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  72. UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();
  73. UIGraphicsEndImageContext();
  74. NSString* docDir = [NSString stringWithFormat:@"%@/Documents/Image", NSHomeDirectory()];
  75. [[NSFileManager defaultManager] createDirectoryAtPath:docDir withIntermediateDirectories:YES attributes:nil error:nil];
  76. NSString *path = [NSString stringWithFormat:@"%@/Documents/Image/IMAGE.PNG", NSHomeDirectory()];
  77. //用png是透明的
  78. [UIImagePNGRepresentation(signatureImage) writeToFile: path atomically:YES];
  79. return signatureImage;
  80. }
  81. @end