0

我正在尝试制作一个可以用手指在当前页面上绘制线条的应用程序。我已经完成了绘图视图,它工作得很好,但是在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()UIGraphicsGetCurrentContext()

4

0 回答 0