我最终使用了Bresenham的线算法(回想起往日,当我不得不编写自己的图形例程时)...
- (void) contextEraseLine:(CGContextRef) ctx from:(CGPoint)startPoint to:(CGPoint) endPoint withThickness:(int)thickness
deltax = abs(x1 - x0);
deltay = abs(y1 - y0);
error = (deltax / 2);
y = y0;
if (x0 > x1)
else
if (y0 > y1)
else
for ((x = x0); (x != (x1 + xstep)); (x += xstep))
(dupe = 0);
if(!dupe)
(error -= deltay);
if (error < 0)
}
}
哎呀!这是一个漫长的过程,只为了创建一个(有些)笨重的橡皮擦线。
要使用它,可以这样做:
- (void)eraseStart {
UIGraphicsBeginImageContext(drawingBoard.size);
ctx = UIGraphicsGetCurrentContext();
CGContextDrawImage(ctx,CGRectMake(0,0,drawingBoard.size.width, drawingBoard.size.height),[drawingBoard CGImage]);
}
- (void)eraseEnd {
drawingBoard = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[drawingView removeFromSuperview];
[drawingView release];
drawingView = [[UIImageView alloc] initWithImage:drawingBoard];
drawingView.frame = CGRectMake(intEtchX, intEtchY, intEtchWidth, intEtchHeight);
[self.view addSubview:drawingView];
}
假设您已经创建了一个drawingView(UIImageView)和drawingBoard(UIImage),要擦除一条线,请执行以下操作:
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self eraseStart];
[self contextEraseLine:ctx from:CGPointMake (x1, y1) to:CGPointMake (x2, y2) withThickness:10];
[self eraseEnd];
(replace x1、y1、x2和y2为适当的值) ...