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 }
时间: 2024-12-16 22:19:39

iOS画图,截图及清空的相关文章

【1】【IOS】 截图

1 UIGraphicsBeginImageContext(self.view.bounds.size);// 设置上下文 2 3 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];4 5 UIImage *image=UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获取image 6 7 UIGraphicsEndImageContext();//关闭上下文

matlab GUI使用subplot画图后如何清空坐标轴

matlab GUI使用subplot画图后如何清空坐标轴: 假设有四个子坐标,分别为h1,h2,h3,h4 h = 0; if ishandle(h1) delete(h1); h = 1; end if ishandle(h2) delete(h2); h = 1; end if ishandle(h3) delete(h3); h = 1; end if ishandle(h4) delete(h4); h1 = 1; end if h axes('parent',handles.uipa

iOS 画图工具的截图

画图工具的截图       (适用于很长的图纸) 点中最底下的最大的背景板,然后command+全部选中 点击PNG以后就是把这个图纸粘贴出来了,复制到 command+n然后 建一个空的表然后cmmand+v就可以了通过这里文件以PNG 的格式另存到桌面就可以了

appium实现截图和清空EditText

前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html   知乎Android客户端登陆:http://www.cnblogs.com/tobecrazy/p/4579631.html 在使用appium的过程中,发现一些appium的坑(后边会详说). adb基本命令总结(Android Debug Bridge) adb 是PC和设备连接的桥梁,可以通过adb对devices进行相关操作

appium实现adb命令 截图和清空EditText

原文地址http://www.cnblogs.com/tobecrazy/p/4592405.html 原文地址http://www.cnblogs.com/tobecrazy/ 该博主有很多干货,可以多去研究研究 adb基本命令总结(Android Debug Bridge) adb 是PC和设备连接的桥梁,可以通过adb对devices进行相关操作 adb devices           列出你的devices adb kill-server         杀掉adb服务(如果设备连接

ios 画图,绘制坐标系,画坐标系

先来看个效果: 新建视图类,在直接添加代码: // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // 获取当前环境 CGContextRef context = UIGraphicsGetCurrentContext()

iOS 画图基础

基础要点: 1,画图不可以在 ViewController 里,而是应该在一个 UIView 的子类中,比如新建一个 DrawView 继承自 UIView. 2,覆盖 UIView 的 drawRect 方法,使得它画符合需要的图. #import <UIKit/UIKit.h> @interface DrawView : UIView @end /*****************************/ #import "DrawView.h" @implement

iOS 画图讲解2

1.图片水印 //layer上下文只能显示在drawRect里 //当开启上下文时,绘制图形即可在viewDidLoad中实现 //位图的上下文 //UIGraphicsBeginImageContext()仅适用于非retina屏 //开启位图上下文 size:位图的尺寸 opaque:不透明是yes,透明就是no scale:是否缩放上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); //绘制原始图片 [image d

ios view截图

- (UIImage *)cutFromView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageCo