UIView drawRect:绘制直线时,rect区域将清除,因此上一个图形将消失

UIView drawRect:绘制直线时,rect区域将清除,因此上一个图形将消失,uiview,drawing,cgcontext,drawrect,Uiview,Drawing,Cgcontext,Drawrect,很难说,所以我上传了一张图片来说明我的问题: 基本上,在drawRect中,我将从作为手指触摸移动的触摸中绘制线,我将调用“needsDisplayInRect”进行重画。但是我发现第一行已经完成了,第二行将清除矩形部分,所以以前的一些绘图已经消失了 以下是我的实现: enter code here -(void) drawRect:(CGRect)rect{ //[super drawRect: rect]; CGContextRef context = UIGraphicsGetCurre

很难说,所以我上传了一张图片来说明我的问题:

基本上,在drawRect中,我将从作为手指触摸移动的触摸中绘制线,我将调用“needsDisplayInRect”进行重画。但是我发现第一行已经完成了,第二行将清除矩形部分,所以以前的一些绘图已经消失了

以下是我的实现:

enter code here

-(void) drawRect:(CGRect)rect{
//[super drawRect: rect];
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawSquiggle:squiggle at:rect inContext:context];
}

- (void)drawSquiggle:(Squiggle *)squiggle at:(CGRect) rect inContext:(CGContextRef)context
{   

CGContextSetBlendMode(context, kCGBlendModeMultiply);
UIColor *squiggleColor = squiggle.strokeColor; // get squiggle's color
CGColorRef colorRef = [squiggleColor CGColor]; // get the CGColor
CGContextSetStrokeColorWithColor(context, colorRef);        

NSMutableArray *points = [squiggle points]; // get points from squiggle
// retrieve the NSValue object and store the value in firstPoint
CGPoint firstPoint; // declare a CGPoint
[[points objectAtIndex:0] getValue:&firstPoint];
// move to the point
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
// draw a line from each point to the next in order

for (int i = 1; i < [points count]; i++)
{
    NSValue *value = [points objectAtIndex:i]; // get the next value
    CGPoint point; // declare a new point
    [value getValue:&point]; // store the value in point

    // draw a line to the new point
    CGContextAddLineToPoint(context, point.x, point.y);
} // end for
CGContextStrokePath(context); 
}

drawRect:
中的
UIGraphicsGetCurrentContext
返回的图形上下文归系统所有,并在绘图过程开始时清除。创建自己的
CGContext
以记录图形,并根据需要将其呈现到
drawRect:
上下文中

创建成对的
CGBitmapContext
CGImage
,它们引用相同的
CGDataProvider
,并且具有相同的大小、颜色空间和其他属性。在您认为合适的时间和方式下绘制该上下文,然后在
drawRect:
中使用
CGContextDrawImage
来显示它

或者,如果您只是在做一系列的工作,您可以构建一个
CGMutablePath
,并将其呈现给系统上下文

编辑:

查看各自头文件中的
CGImageCreate
cgitmapcontextcreate
。大多数论点都是相同的。位图上下文需要原始数据指针,而图像需要CGDataProvider。下面是代码的大致情况。最后,您可以绘制到上下文中,然后使用图像渲染到另一个上下文中

size_t width = 400;
size_t height = 300;
CFMutableDataRef data = CFDataCreateMutable( NULL , width * height * 4 ); // 4 is bytes per pixel
CGDataProviderRef provider = CGDataProviderCreateWithCFData( data );
CGImageRef image = CGImageCreate( width , height , ... , provider , ... );
CGBitmapContextRef context = CGBitmapContextCreate( CFDataGetMutableBytePtr( data ) , width , height , ... );
CFRelease( data ); // retained by provider I think
CGDataProviderRelease( provider ); // retained by image

有很多空白需要填补,但这应该让你开始。您可以使用malloc而不是CFData作为缓冲区。

drawRect:
中的
UIGraphicsGetCurrentContext
返回的图形上下文归系统所有,并在绘图过程开始时清除。创建自己的
CGContext
以记录图形,并根据需要将其呈现到
drawRect:
上下文中

创建成对的
CGBitmapContext
CGImage
,它们引用相同的
CGDataProvider
,并且具有相同的大小、颜色空间和其他属性。在您认为合适的时间和方式下绘制该上下文,然后在
drawRect:
中使用
CGContextDrawImage
来显示它

或者,如果您只是在做一系列的工作,您可以构建一个
CGMutablePath
,并将其呈现给系统上下文

编辑:

查看各自头文件中的
CGImageCreate
cgitmapcontextcreate
。大多数论点都是相同的。位图上下文需要原始数据指针,而图像需要CGDataProvider。下面是代码的大致情况。最后,您可以绘制到上下文中,然后使用图像渲染到另一个上下文中

size_t width = 400;
size_t height = 300;
CFMutableDataRef data = CFDataCreateMutable( NULL , width * height * 4 ); // 4 is bytes per pixel
CGDataProviderRef provider = CGDataProviderCreateWithCFData( data );
CGImageRef image = CGImageCreate( width , height , ... , provider , ... );
CGBitmapContextRef context = CGBitmapContextCreate( CFDataGetMutableBytePtr( data ) , width , height , ... );
CFRelease( data ); // retained by provider I think
CGDataProviderRelease( provider ); // retained by image

有很多空白需要填补,但这应该让你开始。您可以使用malloc而不是CFData作为缓冲区。

我想我知道您的想法了。但我不确定CGDataProvider,是否有这样的示例代码?我有一个实现,但它并没有像我预期的那样工作。我想我的理解还是错的?我已经发布了密码,你介意检查一下吗?我想我知道你的想法了。但我不确定CGDataProvider,是否有这样的示例代码?我有一个实现,但它并没有像我预期的那样工作。我想我的理解还是错的?我已经把代码发出去了,你介意检查一下吗?