私は実際にビューに線を引こうとしています。すべての描画の前にコンテキストをクリアしないようにするために、描画するために独自のコンテキストを作成する必要があることを理解しました。
コンテキストを作成するこの方法を見つけました:
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;
}
しかし、私の質問は次のとおりです。ビューを毎回きれいにしないために、このコンテキストをどのように使用できますか?
EDIT (@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 に設定されます)、線は消えませんが、新しい線を描画しようとすると (各 setNeedsDisplay 呼び出しですべてを再描画しないように isAuthorizeDrawing が NO に設定されます) )、すべての線が消えて、描画が非常に遅くなります.. :/
私は何か間違っていますか?
編集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];
}