iOS开发之自定义画板

  今天整好有时间, 写了一个自定义的画板!  [我的github]

GLPaint主要采用QuartzCore框架, 对画布上的元素进行渲染, 然后通过UIImageWriteToSavedPhotosAlbum保存到相册.

代码耦合性比较低, 集成比较简单, 初学者可以了解一下.

1. 在ViewController中只需要实现这几个方法基本就没问题了;

 1 #pragma mark - 创建colorPiker
 2 - (void)setupColorPiker
 3 {
 4     _piker = [ColorPicker colorPickerWithFrame:CGRectMake(0, 64, PP_SCREEN_WIDTH, 130)];
 5     _piker.delegate = self;
 6     [self.view addSubview:_piker];
 7     
 8 }
 9 
10 
11 #pragma mark - 创建PaintView
12 - (void)setupPaintView
13 {
14     _paintView = [PaintView paintView];
15     _paintView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:.95];
16 
17     // 设置线宽
18     _paintView.lineWidth = 8.0f;
19     [self.view addSubview:_paintView];
20     
21     // 约束
22     [_paintView mas_makeConstraints:^(MASConstraintMaker *make) {
23         make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(CGRectGetMaxY(_piker.frame) , 5, 0, 5));
24     }];
25     
26 }
27 
28 
29 #pragma mark - ColorPickerDelegate
30 - (void)aColorPickerDidSelected:(UIColor *)color
31 {
32     [_paintView setPaintColor:color];
33     
34 }

2. 保存到相册

 1 #pragma mark - 保存图片到相册
 2 // 监听点击
 3 - (void)saveImage
 4 {
 5     // 获取照片
 6     UIImage *paintImage = [_paintView paintImage];
 7     LogRed(@"%@", paintImage);
 8     
 9     UIImageWriteToSavedPhotosAlbum(paintImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
10 }
11 
12 - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
13 {
14     NSString *message = @"";
15     if (!error) {
16         message = @"成功保存到相册";
17     }else
18     {
19         message = [error description];
20     }
21     LogRed(@"message is %@",message);
22 }
原文地址:https://www.cnblogs.com/guangleijia/p/5045992.html