iOS画图,截图及清空

 1 #import "ViewController.h"
 2 #import "SecondVC.h"
 3 
 4 
 5 #define WIDTH [UIScreen mainScreen].bounds.size.width
 6 #define HEIGHT [UIScreen mainScreen].bounds.size.height
 7 
 8 @interface ViewController ()
 9 {
10     UIImageView *_canvasView;//画布
11     CGPoint _startPoint;//记录开始坐标
12     
13 }
14 @end
15 
16 @implementation ViewController
17 
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20     
21     _canvasView = [[UIImageView alloc] initWithFrame:self.view.bounds];
22     [self.view addSubview:_canvasView];
23     
24     UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
25     clearBtn.frame = CGRectMake(50, 20, 100, 30);
26     [clearBtn addTarget:self action:@selector(clearBtnClick) forControlEvents:UIControlEventTouchUpInside];
27     [clearBtn setTitle:@"清空" forState:UIControlStateNormal];
28     clearBtn.backgroundColor = [UIColor orangeColor];
29     [self.view addSubview:clearBtn];
30     
31     UIButton *cutPicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
32     cutPicBtn.frame = CGRectMake(250, 20, 100, 30);
33     [cutPicBtn addTarget:self action:@selector(cutPicBtnClick) forControlEvents:UIControlEventTouchUpInside];
34     [cutPicBtn setTitle:@"截图" forState:UIControlStateNormal];
35     cutPicBtn.backgroundColor = [UIColor orangeColor];
36     [self.view addSubview:cutPicBtn];
37     
38 }
39 
40 
41 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
42 {
43     _startPoint = [[touches anyObject] locationInView:_canvasView];
44 
45 }
46 
47 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
48 {
49     CGPoint movePoint = [[touches anyObject] locationInView:_canvasView];
50 
51 //    移动的点有很多,
52     @autoreleasepool {
53         UIGraphicsBeginImageContext(CGSizeMake(WIDTH, HEIGHT));//开始一个图片的绘图
54         
55         [_canvasView drawRect:_canvasView.frame];//绘制原来已经存在的线条
56         
57         CGContextRef context = UIGraphicsGetCurrentContext();//绘图缓存区,当前的是一张图片
58         CGContextSetLineWidth(context, 5);
59         CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
60         CGContextSetLineCap(context, kCGLineCapRound);//边帽
61         CGContextSetLineJoin(context, kCGLineJoinRound);//缝合
62         
63         CGContextMoveToPoint(context, _startPoint.x, _startPoint.y);
64         CGContextAddLineToPoint(context, movePoint.x, movePoint.y);
65         CGContextStrokePath(context);
66         _canvasView.image = UIGraphicsGetImageFromCurrentImageContext();//获取当前图片绘制区域内的图片
67         
68         UIGraphicsEndImageContext();//关闭图片绘制
69     }
70     
71     _startPoint = movePoint;
72 }
73 
74 
75 #pragma mark - 清除按钮
76 - (void)clearBtnClick
77 {
78     
79     _canvasView.image = nil;//这并不是一个好办法。
80     
81 }
82 
83 #pragma mark - 剪切按钮
84 - (void)cutPicBtnClick
85 {
86     UIGraphicsBeginImageContext(CGSizeMake(WIDTH, HEIGHT));//开始一个图片的绘图
87 //    在屏幕加载之后截图
88     [self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES];
89     
90     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//获取当前图片绘制区域内的图片
91     
92     UIGraphicsEndImageContext();
93     
94     SecondVC *vc = [[SecondVC alloc] init];
95     vc.image = image;
96     [self presentViewController:vc animated:YES completion:nil];
97 }

 

posted @ 2016-08-15 17:31  kkkore  阅读(2265)  评论(0编辑  收藏  举报