0

私はついに、使用して常にクリアされなかったDrawRectを取得することができました(私はsetClearsContextBeforeDrawing:YESも持っています)

- (void)drawRect:(CGRect)rect
{
    UIGraphicsPushContext(drawingContext);
    CGImageRef cgImage = CGBitmapContextCreateImage(drawingContext); 
    UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];

    CGContextSetLineWidth(drawingContext, 4.0);
    if (draw)
        CGContextSetStrokeColorWithColor(drawingContext, [UIColor  whiteColor] CGColor]);
    else
        CGContextSetStrokeColorWithColor(drawingContext, [[UIColor clearColor] CGColor]);

    CGContextMoveToPoint(drawingContext,    lastPt.x - (31.0 / self.transform.a),  lastPt.y - (31.0 / self.transform.a)  );
    CGContextAddLineToPoint(drawingContext, currPt.x - (31.0 / self.transform.a),  currPt.y - (31.0 / self.transform.a)  );
    CGContextStrokePath(drawingContext);

    UIGraphicsPopContext();
    CGImageRelease(cgImage);
    [uiImage drawInRect: rect];

    lastPt = currPt;
}

これにより、描画時に線が
残ります。私の問題は、描画がNOの場合です[UIColor clearColor]は消去されません。消去には何を使用する必要がありますか?前もって感謝します

4

1 に答える 1

2

はアルファがゼロであるためclearColor、通常、それを使用して描画しても効果はありません。有効にするには、コンテキストのブレンド モードを に設定する必要がありますkCGBlendModeCopy

ただし、コンテキストのブレンド モードkCGBlendModeClearを消去したいときに設定しkCGBlendModeNormal、描画したいときに戻す方が簡単です。

if (draw) {
    CGContextSetBlendMode(drawingContext, kCGBlendModeNormal);
    CGContextSetStrokeColorWithColor(drawingContext, [UIColor  whiteColor] CGColor]);
} else {
    CGContextSetBlendMode(drawingContext, kCGBlendModeClear);
}
于 2012-06-24T19:25:50.083 に答える