我正在制作绘画应用程序,但在制作画笔纹理时遇到了麻烦。
我正在使用Core Graphics在touchesBegan
和touchesMoved
上绘制粗线和圆,我希望它具有更平滑的纹理,就像真实的画笔一样。使用Core Graphics有可能吗?
这是我的代码:
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();
最佳答案
我建议将OpenGL用于绘画应用程序。查看“GLPaint”示例。它正是您在OpenGL中寻找的东西。 (在CG的帮助下)。如果只想绘制到屏幕或位图表示,则openGL应该更快。
如果要坚持使用CG,则应创建一个CGLayer,在图层中绘制画笔纹理,然后将图层重复绘制到图像所在的最终上下文中。
查看Docu中的CGLayerCreate,CGContextDrawLayerAtPoint。
您将需要一些代码,通过以规则的间隔在该路径上找到一个点来对要绘制的路径进行采样,然后在这些点上绘制画笔形状。据我所知,CG中不存在此类功能或方法,因此您必须编写自己的代码。它对于线条来说应该不难,但是它涉及贝塞尔曲线或三次方路径的一些数学运算。
关于objective-c - 使用Core Graphics制作笔刷般的纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8117830/