天天看點

IOS 畫圖闆-使用UIBezierPath

IOS 畫圖闆-使用UIBezierPath

uiviewcontroller.m

//
//  ViewController.m
//  06-畫圖
//
//  Created by panba on 16-5-23.
//  Copyright (c) 2016年 panba. All rights reserved.
//

#import "ViewController.h"
#import "JCView.h"
@interface ViewController ()
@property(strong,nonatomic) JCView *myview;

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //1-添加view
    JCView *myview = [[JCView alloc]initWithFrame:CGRectMake(0, 100, 320, 320)];
    myview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:myview];
    self.view.backgroundColor = [UIColor grayColor];
    self.myview = myview;
    //2-添加清屏,回退,儲存按鈕
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    clearBtn.frame = CGRectMake(20, 60, 50, 44);
    [clearBtn setTitle:@"清屏" forState:UIControlStateNormal];
    [clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:clearBtn];
    [clearBtn addTarget:self action:@selector(clearClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    backBtn.frame = CGRectMake(100, 60, 50, 44);
    [backBtn setTitle:@"回退" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    saveBtn.frame = CGRectMake(180, 60, 50, 44);
    [saveBtn setTitle:@"儲存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:saveBtn];
    [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
}
-(void)clearClick
{
    [self.myview clearClick];
}
-(void)backClick
{
    [self.myview backClick];
}
-(void)saveClick
{
    //1-得到bit上下文
     UIGraphicsBeginImageContext(self.myview.frame.size) ;
    //2-繪圖到上下文上
    [self.myview.layer renderInContext:UIGraphicsGetCurrentContext()];
    //3-得到新圖檔
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4-儲存
    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        NSLog(@"儲存失敗");
    }
    else
    {
        NSLog(@"儲存成功");
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
           

JCView.h

//
//  JCView.h
//  06-畫圖
//
//  Created by panba on 16-5-23.
//  Copyright (c) 2016年 panba. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface JCView : UIView
-(void)clearClick;
-(void)backClick;
-(void)saveClick;

@end
           

JCView.m

//
//  JCView.m
//  06-畫圖
//
//  Created by panba on 16-5-23.
//  Copyright (c) 2016年 panba. All rights reserved.
//

#import "JCView.h"

@interface JCView ()
@property (nonatomic,strong) NSMutableArray *totalPoint;

@end


@implementation JCView

//懶加載
-(NSMutableArray *)totalPoint
{
    if (_totalPoint == nil) {
        _totalPoint = [NSMutableArray array];
    }
    return _totalPoint;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //1-得到開始點
    CGPoint startPoint = [touch locationInView:touch.view];
//    //2-建立小數組
//    NSMutableArray *subtarr = [NSMutableArray array];
//    [subtarr addObject:[NSValue valueWithCGPoint:startPoint]];
//    //3-将小數組添加到大數組
//    [self.totalPoint addObject:subtarr];
    //2-建立路徑
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:startPoint];
    //3-添加到數組
    [self.totalPoint addObject:path];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //1-得到停止點
    CGPoint movePoint = [touch locationInView:touch.view];
//    //2-得到小數組
//    NSMutableArray *subtarr = [self.totalPoint lastObject];
//    [subtarr addObject:[NSValue valueWithCGPoint:movePoint]];
//    
//    //4-渲染
//    [self setNeedsDisplay];
    //2-得到路徑
    UIBezierPath *currpath = [self.totalPoint lastObject];
    [currpath addLineToPoint:movePoint];
    //4-渲染
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //1-得到目前點
    CGPoint endPoint = [touch locationInView:touch.view];
//    //2-得到小數組
//    NSMutableArray *subarr = [self.totalPoint lastObject];
//    [subarr addObject:[NSValue valueWithCGPoint:endPoint]];
    //2-得到路徑
    UIBezierPath *currpath = [self.totalPoint lastObject];
    [currpath addLineToPoint:endPoint];
    
    //4-渲染
    [self setNeedsDisplay];
    
}


- (void)drawRect:(CGRect)rect
{
    for (UIBezierPath *path in self.totalPoint) {
        [path stroke];
    }
}
-(void)test
{
    // Drawing code
    //1-擷取上下文
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    
    //2-劃線
    for (NSMutableArray *subarr in self.totalPoint) {
        for (int index = 0; index <subarr.count; index++) {
            CGPoint point = [subarr[index] CGPointValue];
            if (0 == index) {
                CGContextMoveToPoint(ctx, point.x, point.y);
            }
            else
            {
                CGContextAddLineToPoint(ctx, point.x, point.y);
            }
        }
    }
    //    CGContextMoveToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>);
    //    CGContextAddLineToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>);
    
    //3-渲染
    CGContextStrokePath(ctx);
}
 
-(void)clearClick
{
    [self.totalPoint removeAllObjects];
    [self setNeedsDisplay];
}
-(void)backClick
{
    [self.totalPoint removeLastObject];
    [self setNeedsDisplay];
}
-(void)saveClick
{
   
}

@end