Question

I want to draw nil instead of the colors, because in the end I want the code to erase a section of the UIImageView in order that you can see what is behind the 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++;

and

- (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();
        }
    }
}
Was it helpful?

Solution

Colin Gislason's answer is incorrect. If you set drawingColorAlpha to 0, it just paints an invisible color on the existing image.

Here is what works.

//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)); 

OTHER TIPS

What you want to do is set drawingColorAlpha to 0. This creates a transparent color which is equivalent to erasing. You might also have to set the blend mode using CGContextSetBlendMode(). kCGBlendModeNormal should work.

Set blend mode as follows: CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

Hope it will help u !! :P

What you want is not nil, but a color that's transparent. If your UIImageView isn't marked as "Opaque" in IB, then it should show through in any areas where the pixels are transparent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top