星空网 > 软件开发 > 操作系统

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 @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<UITouch *> *)touches withEvent:(UIEvent *)event42 {43   _startPoint = [[touches anyObject] locationInView:_canvasView];44 45 }46 47 - (void)touchesMoved:(NSSet<UITouch *> *)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 }

 




原标题:iOS画图,截图及清空

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流