我有以下代码,但它并不流畅。如果我画一个圆,我会看到尖锐的角。
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
发布于 2011-01-23 11:56:19
你会看到一些锐角,因为你画了太多短的抗锯齿线段,所有这些都花费了超过1/60秒的时间,因此你最终错过了UI触摸事件更新,这导致了更参差不齐的路径。
2D CG绘图在iOS设备上不会加速。
如果你想坚持使用CG绘图,试着只绘制最后4个左右的线段,也许可以关闭抗锯齿,看看这样做是否更平滑。然后在路径图绘制完成后填写剩下的部分(修饰)。
发布于 2011-01-23 18:13:14
即使使用完整的60FPS,你也会得到边缘而不是曲线。使用CG最好的方法是使用bezier路径。在CG之外,样条线。
发布于 2011-01-24 06:19:15
您需要做的不是每次触摸移动时都调用绘制函数,而是创建一个累加器,并在每次调用时递增它。如果达到某个阈值,则执行绘图代码。但您绝对应该在第一次调用该方法时运行它。要找到一个好的阈值,您必须对其进行实验。
static int accum = 0;
if ((accum == 0) || (accum == threshold)) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
accum = 0;
}
accum++;
https://stackoverflow.com/questions/4772099
复制相似问题