1

我想绘制nil而不是颜色,因为最后我希望代码擦除一部分,UIImageView以便您可以看到UIImageView.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.x = currentPoint.x;
    currentPoint.y = currentPoint.y;
    mouseSwiped = YES;
    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal);
    CGContextSetAlpha(UIGraphicsGetCurrentContext(), drawingColorAlpha);
    CGContextSaveGState(UIGraphicsGetCurrentContext());    //Probably right here
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
            drawingColorRed, // I want to draw nil here instead of these CGFloats that I have made
            drawingColorGreen, // I want to draw nil here instead of these CGFloats that I have made
            drawingColorBlue, // I want to draw nil here instead of these CGFloats that I have made
            drawingColorAlpha); // I want to draw nil here instead of these CGFloats that I have made
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(),
                                          lastPoint.x,
                                          lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 
                                           currentPoint.x,
                                           currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing my context to
    NSLog(@"current point x: %d current point y: %d",currentPoint.x, currentPoint.y); //Used for my purposes
    lastPoint = currentPoint;
    mouseMoved++;

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    if (!optionsDisplayerVisible && canDraw)
    {
        if(!mouseSwiped)
        {
            UIGraphicsBeginImageContext(self.view.frame.size);
            [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal);
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
                                       drawingColorRed,
                                       drawingColorGreen,
                                       drawingColorBlue,
                                       drawingColorAlpha);
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            CGContextFlush(UIGraphicsGetCurrentContext());
            drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing to
            UIGraphicsEndImageContext();
        }
    }
}
4

4 回答 4

1

Colin Gislason 的回答不正确。如果设置drawingColorAlpha为 0,它只会在现有图像上绘制不可见的颜色。

这是有效的。

//set this at the beginning
CGContextSetLineCap(ctx,kCGImageAlphaNone);
//after moving the point to your pen location:
CGContextClearRect (ctx, CGRectMake(lastPoint.x, lastPoint.y, 10, 10)); 
于 2010-01-31T09:17:59.800 回答
0

您想要做的是设置drawingColorAlpha为 0。这将创建一个透明的颜色,相当于擦除。您可能还必须使用CGContextSetBlendMode(). kCGBlendModeNormal应该管用。

于 2009-12-22T21:55:23.893 回答
0

设置混合模式如下:CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

希望它会帮助你!:P

于 2011-02-05T11:03:58.950 回答
-1

你想要的不是零,而是一种透明的颜色。如果您的 UIImageView 在 IB 中未标记为“不透明”,则它应该在像素透明的任何区域中显示出来。

于 2009-12-22T21:26:57.403 回答