我正在尝试创建一个可以在当前页面上用手指绘制线条的应用程序。我已经完成了绘图视图,它运行良好,但在{{1}之后存在严重的延迟(慢)问题在当前的背景下被多次绘制。
我已经搜索过这个问题了,似乎bufferlayer
每次调用CGLayer
时都会自动重绘。
还有我的代码,看看是否有人可以帮助我提高绘图速度,谢谢。
CGContextDrawLayerInRect(boardcontext,[self bounds],bufflayer)
我还尝试创建- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor=[UIColor clearColor];
self.points=[NSMutableArray array];
boardcontext=UIGraphicsGetCurrentContext();
layer=CGLayerCreateWithContext(boardcontext, self.frame.size, NULL);
}
return self;
}
-(void)drawLayer{
CGContextRef context=CGLayerGetContext(layer);
for (int i=points.count-2; i<points.count-1; i++) {
CGPoint pt1=[[self.points objectAtIndex:i]CGPointValue];
CGPoint pt2=[[self.points objectAtIndex:i+1]CGPointValue];
CGContextMoveToPoint(context, pt1.x, pt1.y);
CGContextAddLineToPoint(context, pt2.x, pt2.y);
}
CGContextStrokePath(context);
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
boardcontext=UIGraphicsGetCurrentContext();
CGContextDrawLayerInRect(boardcontext, rect, layer);
// CGContextDrawLayerAtPoint(boardcontext,CGPointZero,layer);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[points removeAllObjects];
CGPoint pt = [[touches anyObject]locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint pt = [[touches anyObject]locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
[self drawLayer];
[self setNeedsDisplay];
}
作为缓冲区,而不是在CGbitmapcontext
之外调用UIGraphicsGetCurrentContext()
,
但这似乎比使用drawRect()
更糟糕。