Reputation: 229
I am making a painting app, and I'm having trouble making a brush texture.
I am drawing a thick line and circle on touchesBegan
and touchesMoved
using Core Graphics, and I want it to have a smoother texture, like a real brush. Is this possible using Core Graphics?
This is my code:
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 35.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),
redAmt, blueAmt, greenAmt, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(),
endingPoint.x, endingPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),
currentTouch.x, currentTouch.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Upvotes: 1
Views: 2135
Reputation: 1541
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
UIGraphicsBeginImageContext(imageView1.frame.size);
[imageView1.image drawInRect:CGRectMake(0, 0,imageView1.frame.size.width, imageView1.frame.size.height)];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(point.x, point.y, 40, 40));
CGContextStrokePath(UIGraphicsGetCurrentContext());
imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Upvotes: -1
Reputation: 6767
I would suggest to use OpenGL for a painting application. Look at the "GLPaint" Example. It does just what you are looking for in OpenGL. (With a little help of CG). If you just want to draw to the screen, or to bitmap representations, openGL should be much faster.
If you what to stick with CG, you should create a CGLayer, draw you brush texture in the layer and then draw the layer repeatedly into the final context you what your image in.
Look at the Docu for CGLayerCreate, CGContextDrawLayerAtPoint.
You would need some code, that samples the path you want to draw by finding point on that path in a regular interval and then draw you brush shape at those points. To my knowledge, no such functions or methods exist in CG, so you have to code your own. It's shouldn't be hard for lines, but it involves some math for bezier or cubic paths.
Upvotes: 3
Reputation: 16861
The simple way to implement a brush tool is to use an image and repeatedly composite it into the drawing context.
Upvotes: 0