如何使用cgcontext绘制nil

时间:2009-12-22 21:16:57

标签: iphone uiimageview cgcontext null

我想绘制nil而不是颜色,因为最后我希望代码删除UIImageView的一部分,以便您可以看到{{1}背后的内容}。

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

4 个答案:

答案 0 :(得分: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)); 

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

希望它会对你有所帮助!! :P

答案 3 :(得分:-1)

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