畫板畫圖
{
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;
}
}