gpt4 book ai didi

objective-c - 绘制自定义上下文

转载 作者:行者123 更新时间:2023-12-01 18:25:39 25 4
gpt4 key购买 nike

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

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

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;
}

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

编辑 (在@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];
}

最佳答案

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

当您在 View 提供的上下文中绘图时(即在 drawRect: 内), View 每次都在创建一个新的上下文,或者它已经删除了上下文的内容,例如摇动 Etch-a-Sketch。无论哪种方式,您都会得到一个(至少有效地)没有绘制任何内容的上下文。

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

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

大概你想不时地向用户展示你到目前为止绘制的内容(即,当 drawRect: 发生时)。要做到这一点,ask the bitmap context to create an image of its contentsdraw that image进入 the current (UIKit-supplied) context .

或者,只需 tell the view not to clear itself before every draw ,并且根本不用费心管理自己的位图上下文。

关于objective-c - 绘制自定义上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14022872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com