画图3

画板画图


{

UIImageView *_canvasImageView;

//起点

CGPoint _startPoint;

}

- (void)viewDidLoad {

[super viewDidLoad];

_canvasImageView = [[UIImageView alloc] init];

_canvasImageView.frame = self.view.frame;

[self.view addSubview:_canvasImageView];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

//开始触摸记录初始触摸点坐标

_startPoint = [[touches anyObject] locationInView:_canvasImageView];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

//移动点坐标

CGPoint movePoint = [[touches anyObject] locationInView:_canvasImageView];

//释放池

@autoreleasepool {

UIGraphicsBeginImageContext(_canvasImageView.bounds.size);

[_canvasImageView drawRect:_canvasImageView.bounds];

CGContextRefcontext = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 20);

CGContextSetRGBStrokeColor(context, 0, 1, 0.5, 1);

CGContextSetLineCap(context,kCGLineCapRound);

CGContextSetLineJoin(context,kCGLineJoinRound);

CGContextMoveToPoint(context,_startPoint.x,_startPoint.y);

CGContextAddLineToPoint(context, movePoint.x, movePoint.y);

CGContextStrokePath(context);

UIImage *image  =UIGraphicsGetImageFromCurrentImageContext();

_canvasImageView.image = image;

UIGraphicsEndPDFContext();

_startPoint = movePoint;

}

}

你可能感兴趣的:(画图3)