一:自定义view .h文件中代码如下
#import <UIKit/UIKit.h> @interface ZLpaintView : UIView @property(nonatomic, strong) UIColor *currentColor; - (void)back; - (void)clear; - (void)savetoFile:(NSString *)file; @end
.m中如下
#import "ZLpaintView.h" @interface ZLpaintView() //用于存放 存放某条线的点 的数组 @property (nonatomic, strong) NSMutableArray *pointsOfAllLines; //用于存放每条线的颜色 @property (nonatomic, strong) NSMutableArray *colorsOfAllLines; @end @implementation ZLpaintView - (NSMutableArray *)pointsOfAllLines { if (!_pointsOfAllLines) { _pointsOfAllLines = [NSMutableArray array]; } return _pointsOfAllLines; } - (NSMutableArray *)colorsOfAllLines { if (!_colorsOfAllLines) { _colorsOfAllLines = [NSMutableArray array]; } return _colorsOfAllLines; } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code CGContextRef context = UIGraphicsGetCurrentContext(); // 设置线宽和收尾及连接点的样式 CGContextSetLineWidth(context, 8); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineJoin(context, kCGLineJoinRound); // 遍历所有线 NSInteger countOfLines = self.pointsOfAllLines.count; // 取出每条线 for (NSInteger i = 0; i < countOfLines; i ++) { NSArray *pointsOfline = self.pointsOfAllLines[i]; NSInteger countOfPoints = pointsOfline.count; //设置这条线的颜色 //取出对应线的颜色 UIColor *currentColor = self.colorsOfAllLines[i]; [currentColor set]; //遍历这条线里的两个点 for (NSInteger j = 0; j < countOfPoints; j ++) { CGPoint location = [pointsOfline[j] CGPointValue]; if (j == 0) { CGContextMoveToPoint(context,location.x,location.y); }else{ CGContextAddLineToPoint(context,location.x,location.y); } } CGContextStrokePath(context);//每画完一条线,渲染一次 } } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 一触摸屏幕,就建立一个数组用于存储一条线的点。 NSMutableArray *pointsOfLine = [NSMutableArray array]; // 生成线后,将他的对应颜色也放进数组里 if (!self.currentColor) { self.currentColor = [UIColor blackColor];//如果当前颜色为空,则设置黑色 [self.colorsOfAllLines addObject:self.currentColor]; }else { [self.colorsOfAllLines addObject:self.currentColor];//否则直接加入到这个数组中 } // 将生成的数组存放在自己属性数组中。 [self.pointsOfAllLines addObject:pointsOfLine]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; // NSLog(@"%@",[NSValue valueWithCGPoint:location]); NSMutableArray * pointsOfLine = [self.pointsOfAllLines lastObject]; [pointsOfLine addObject:[NSValue valueWithCGPoint:location]]; NSLog(@"%@",pointsOfLine); [self setNeedsDisplay]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%ld",self.pointsOfAllLines.count); } - (void)back { [self.pointsOfAllLines removeLastObject]; [self setNeedsDisplay]; } - (void)clear { [self.pointsOfAllLines removeAllObjects]; [self setNeedsDisplay]; } - (void)savetoFile:(NSString *)file { UIGraphicsBeginImageContext(self.bounds.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData *imageDate = UIImagePNGRepresentation(newImage); [imageDate writeToFile:file atomically:YES]; } @end
二:在storyboard拖相应控件,并在控制器中实现相应地方法,代码如下:
#import "ViewController.h" #import "ZLpaintView.h" @interface ViewController () @property (weak, nonatomic) IBOutlet ZLpaintView *paintView; - (IBAction)backClick; - (IBAction)clearClick; - (IBAction)saveClick; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)backClick { [self.paintView back]; } - (IBAction)clearClick { [self.paintView clear]; NSLog(@"%@",self.paintView.currentColor); } - (IBAction)saveClick { [self.paintView savetoFile:@"/Users/mac/Desktop/TheImage.png"]; } - (IBAction)colorBtnClick:(UIButton *)sender { // 设置当前的颜色 self.paintView.currentColor = sender.backgroundColor; } @end
三:效果: