IOS开发之——画板-清屏/撤销/橡皮擦/保存(3)

一 概述

画板——清屏,撤销,橡皮擦,保存

二 清屏

2.1 说明

  • PaintView方法提供clearScreen清屏方法,将paths中的所有内容情况,并重绘
  • ViewController方法调用PaintView中的clearScreen清屏

2.2 代码

PaintView.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)drawRect:(CGRect)rect {
// Drawing code
if (!self.paths.count) {
return;
}
for (PaintPath *path in self.paths) {
[path.color set];
[path stroke];
}
}
-(void)clearScreen
{
[self.paths removeAllObjects];
//重绘
[self setNeedsDisplay];
}

ViewController.m

1
2
3
4
5
//清屏
- (IBAction)clearScreen:(UIBarButtonItem *)sender
{
[_paintView clearScreen];
}

2.3 效果图

三 撤销

3.1 说明

  • 从paths中移除上一次的操作,并重绘

3.2 代码

PaintView.m

1
2
3
4
5
6
-(void)undo
{
[self.paths removeLastObject];
//重绘
[self setNeedsDisplay];
}

ViewController.m

1
2
3
4
5
//撤销
- (IBAction)undo:(UIBarButtonItem *)sender
{
[_paintView undo];
}

3.3 效果图

四 橡皮擦

4.1 说明

  • 将whiteColor赋值给PaintView的color

4.2 代码

1
2
3
4
5
//橡皮擦
- (IBAction)eraser:(UIBarButtonItem *)sender
{
_paintView.color=[UIColor whiteColor];
}

4.3 效果图

五 保存相册

5.1 功能说明

  • 申请相册操作的权限
  • 把画板截屏并保存到相册中
  • 保存成功或失败,使用MBProgress显示操作结果

5.2 功能代码

info.plist(右键——Open as Source Code)

1
2
3
4
5
6
7
8
9
<!-- 相册 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>
<!-- 相机 -->
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能访问相机</string>
<!-- 媒体资料库 -->
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能访问媒体资料库</string>

MBProgress引用

1
2
3
4
5
6
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// Do something...
[MBProgressHUD hideHUDForView:self.view animated:YES];
});

ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//保存
- (IBAction)save:(UIBarButtonItem *)sender
{
//把画板截屏
//1-开启上下文
UIGraphicsBeginImageContextWithOptions(_paintView.bounds.size, NO, 0.0);
//获取当前上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//把画板上的内容渲染到上下文
[_paintView.layer renderInContext:ctx];
//获取新的图片
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
//保存到用户的相册里面
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//保存相册后回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) { //保存失败

MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.label.text = @"保存失败";
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[MBProgressHUD hideHUDForView:self.view animated:YES];
});

//[MBProgressHUD :@"保存失败"];
NSLog(@"保存失败");
}else{ //保存成功

MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.label.text = @"保存成功";
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
NSLog(@"保存成功");
}
}

5.3 效果图