IOS drawing, screenshot and clear, ios drawing

Source: Internet
Author: User

IOS drawing, and clearing, ios drawing

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; // canvas 11 CGPoint _ startPoint; // record start coordinate 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: @ "clear" 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 // There are many moving points, 52 @ autoreleasepool {53 UIGraphicsBeginImageContext (CGSizeMake (WIDTH, HEIGHT); // start the drawing of an image 54 55 [_ canvasView drawRect: _ canvasView. frame]; // draw the existing line 56 57 CGContextRef context = UIGraphicsGetCurrentContext (); // In the drawing cache, the current image is 58 CGContextSetLineWidth (context, 5 ); 59 CGContextSetRGBStrokeColor (context, 1, 0, 0, 1); 60 CGContextSetLineCap (context, kCGLineCapRound); // edge Cap 61 CGContextSetLineJoin (context, kCGLineJoinRound ); // stitch 62 63 CGContextMoveToPoint (context, _ startPoint. x, _ startPoint. y); 64 CGContextAddLineToPoint (context, movePoint. x, movePoint. y); 65 CGContextStrokePath (context); 66 _ canvasView. image = UIGraphicsGetImageFromCurrentImageContext (); // retrieve the image 67 68 UIGraphicsEndImageContext () in the current image painting area; // close the image painting 69} 70 71 _ startPoint = movePoint; 72} 73 74 75 # pragma mark-Clear button 76-(void) clearBtnClick77 {78 79 _ canvasView. image = nil; 80 81} 82 83 # pragma mark-cut button 84-(void) cutPicBtnClick85 {86 UIGraphicsBeginImageContext (CGSizeMake (WIDTH, HEIGHT )); // start drawing an image. 87 // 88 [self. view drawViewHierarchyInRect: self. view. frame afterScreenUpdates: YES]; 89 90 UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); // obtain the image 91 92 UIGraphicsEndImageContext () in the current image painting area (); 93 94 SecondVC * vc = [[SecondVC alloc] init]; 95 vc. image = image; 96 [self presentViewController: vc animated: YES completion: nil]; 97}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.