【问题标题】:How to draw nil using cgcontext如何使用 cgcontext 绘制 nil
【发布时间】:2009-12-22 21:16:57
【问题描述】:

我想绘制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();
        }
    }
}

【问题讨论】:

    标签: iphone uiimageview cgcontext null


    【解决方案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)); 
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        希望对你有帮助!! :P

        【讨论】:

          【解决方案4】:

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

          【讨论】:

          • 不,实际上我希望它擦除 UIImageView 的一部分
          • 我认为你不明白我在告诉你什么。如果你在图像视图中绘制透明像素,它后面的任何东西都会显示出来。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多