views:

305

answers:

3

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();
         }
     }
}
A: 

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.

Mark Bessey
No, actually I want it to erase part of the UIImageView
Jaba
I think you're not understanding what I'm telling you. If you draw transparent pixels into your image view, whatever's behind it will show through.
Mark Bessey
A: 

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.

Colin Gislason
+1  A: 

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.

No. This does not work. If you set drawingColorAlpha to 0, it just paints an invisible color on the existing image. Please do not mislead people by ridiculous and untrue answers.

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