博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS画图,截图及清空
阅读量:6913 次
发布时间:2019-06-27

本文共 3254 字,大约阅读时间需要 10 分钟。

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 @end15 16 @implementation ViewController17 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
*)touches withEvent:(UIEvent *)event42 {43 _startPoint = [[touches anyObject] locationInView:_canvasView];44 45 }46 47 - (void)touchesMoved:(NSSet
*)touches withEvent:(UIEvent *)event48 {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)clearBtnClick77 {78 79 _canvasView.image = nil;//这并不是一个好办法。80 81 }82 83 #pragma mark - 剪切按钮84 - (void)cutPicBtnClick85 {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 }

 

转载于:https://www.cnblogs.com/kkkore/p/5773772.html

你可能感兴趣的文章
面试了若干位Java后端的候选人,给广大程序员的一点建议
查看>>
ios 高德地图
查看>>
php PSR 1 basic coding standard
查看>>
小米手机(小米6)调试时无法安装应用
查看>>
Java springcloud B2B2C o2o多用户商城 springcloud架构
查看>>
从普通程序员到身价过百亿:追求长期价值的耐心,决定了你能走多远 原
查看>>
回顾2017,容器圈热闹的一年
查看>>
spring cloud构建互联网分布式微服务云平台-服务提供与调用
查看>>
python爬虫系列(3.7-使用 bs4 爬取获取贵州农产品)
查看>>
雷声大雨点小,廖翔廖翔你真diao
查看>>
php-fpm服务启动脚本
查看>>
记jpa、json问题之:handler and unable to find...by id ..
查看>>
深度优先和广度优先遍历及其 Java 实现
查看>>
CentOS6.5安装OCSNG2.1.1.1+GLPI0.85—GLPI安装(二)
查看>>
“师傅”与“师父”
查看>>
mybatis错误:[Err] 1064 - You have。。。。 the right syntax to use near 'Delete)
查看>>
Hadoop伪分布式搭建
查看>>
SSL
查看>>
mysql内置函数
查看>>
邮件发送问题
查看>>