1

我实际上是在尝试在视图上画线。为了在每次绘制之前不清除上下文,我知道我必须创建自己的上下文才能绘制它。

我找到了这种创建上下文的方法:

CGContextRef MyCreateBitmapContext (int pixelsWide,
                                int pixelsHigh)
{
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapData = calloc( bitmapByteCount, sizeof(int) );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    if (context== NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );

    return context;
}

但我的问题是:我怎样才能使用这个上下文来避免每次都清理我的视图?

编辑 (在@Peter Hosey 的回答之后)

我尝试做类似的事情:

- (void)drawRect:(CGRect)rect {
    // Creation of the custom context
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), cgImage);
    CGImageRelease(cgImage);
    if (isAuthorizeDrawing) {
         [self drawInContext:context andRect:rect]; // Method which draw all the lines sent by the server
         isAuthorizeDrawing = NO;
    }

    // Draw the line
    [currentDrawing stroke];
}

我还将 UIView 的 clearsContextBeforeDrawing 设置为 NO。

当我缩放时(isAuthorizeDrawing 设置为 YES 以重绘所有正确缩放的线条),线条不会消失,但是当我尝试绘制新线条时(isAuthorizeDrawing 设置为 NO 以便在每次 setNeedsDisplay 调用时不重绘所有内容),所有的线条都消失了,绘图速度真的很慢..:/

难道我做错了什么 ?

编辑 2

这是我的绘图方法:

-(void)drawInContext:(CGContextRef)context {
    for (int i = 0; i < self.drawings.count; ++i) {
        Drawing* drawing = [self.drawings objectAtIndex:i];

        CGContextSetStrokeColorWithColor(context, drawing.colorTrait.CGColor);
        CGContextSetLineWidth(context, 1.0);

        CGContextMoveToPoint(context, [[drawing.points objectAtIndex:0] CGPointValue].x * self.zoomScale, [[drawing.points objectAtIndex:] CGPointValue].y * self.zoomScale);

        for (int i = 1; i < drawing.points.count; i++) {
            CGContextAddLineToPoint(context, [[drawing.points objectAtIndex:i] CGPointValue].x * self.zoomScale, [[drawing.points objectAtIndex:i] CGPointValue].y * self.zoomScale);
        }

        CGContextStrokePath(context);
    }
}

-(void)drawRect:(CGRect)rect {
    if (isRedrawing) {
        [self drawInContext:UIGraphicsGetCurrentContext()];
        isRedrawing = NO;
    }

    [[UIColor redColor] set];
    [currentPath stroke];
}
4

1 回答 1

4

您的上下文为您保留了所有绘图。

当您绘制到由视图提供的上下文时(即,在 内drawRect:),视图要么每次都在创建一个新的上下文,要么它已经删除了上下文的内容,例如摇动 Etch-a-Sketch。无论哪种方式,你都会得到一个(至少实际上)没有被绘制的上下文。

当您绘制到上下文中时,假设在两次使用之间没有做任何事情来擦除它,那么您绘制到其中的所有内容都会堆积起来。

这样做的一个后果是,您需要注意绘制状态,如当前的转换矩阵、剪切路径等,因为在绘制会话之间没有重置这些参数。这可能很有用,具体取决于您在做什么,但无论哪种方式,您都需要了解它并做出相应的计划。

大概你想不时地向用户展示你到目前为止所绘制的内容(即,何时drawRect:发生)。为此,请让位图上下文创建其内容的图像并将该图像绘制当前(UIKit 提供的)上下文中

或者,只是告诉视图在每次绘制之前不要清除自己,并且根本不用费心管理自己的位图上下文。

于 2012-12-24T18:33:24.463 回答