iOS开发之自定义画板

时间:2021-08-01 21:29:11

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

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

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

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

 #pragma mark - 创建colorPiker
- (void)setupColorPiker
{
_piker = [ColorPicker colorPickerWithFrame:CGRectMake(, , PP_SCREEN_WIDTH, )];
_piker.delegate = self;
[self.view addSubview:_piker]; } #pragma mark - 创建PaintView
- (void)setupPaintView
{
_paintView = [PaintView paintView];
_paintView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:.]; // 设置线宽
_paintView.lineWidth = 8.0f;
[self.view addSubview:_paintView]; // 约束
[_paintView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(CGRectGetMaxY(_piker.frame) , , , ));
}]; } #pragma mark - ColorPickerDelegate
- (void)aColorPickerDidSelected:(UIColor *)color
{
[_paintView setPaintColor:color]; }

2. 保存到相册

 #pragma mark - 保存图片到相册
// 监听点击
- (void)saveImage
{
// 获取照片
UIImage *paintImage = [_paintView paintImage];
LogRed(@"%@", paintImage); UIImageWriteToSavedPhotosAlbum(paintImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
} - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSString *message = @"";
if (!error) {
message = @"成功保存到相册";
}else
{
message = [error description];
}
LogRed(@"message is %@",message);
}