acerbetti / ACEDrawingView

An open source iOS component to create a drawing app
Apache License 2.0
665 stars 171 forks source link

Eraser tool with background color #8

Open JohnWickham opened 11 years ago

JohnWickham commented 11 years ago

If the background color is anything but clear, the eraser tool draws like a normal pen tool

ghost commented 11 years ago

+1

prrane commented 11 years ago

well, that is how it's implemented! Eraser in real world is nothing but a magical pen which gives back you White Paper to scratch more! Here, Eraser and a White Colored Pen are abstractions used to understand layman in a proper Domain Terms; I hope this clears the confusion.

0xPr0xy commented 10 years ago

To be able to set the background color to white, and still use the eraser tool change the following pieces of code:

In ACEDrawingTools.m

- (void)draw
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextAddPath(context, path);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);

    // disable blend mode and set stroke color to white since we do not want a transparent bg
    // https://github.com/acerbetti/ACEDrawingView/issues/8
    // CGContextSetBlendMode(context, kCGBlendModeClear);

    CGContextStrokePath(context);
    CGContextRestoreGState(context);
}

In ACEDrawingView.m

- (void)configure
{
    // init the private arrays
    self.pathArray = [NSMutableArray array];
    self.bufferArray = [NSMutableArray array];

    // set the default values for the public properties
    self.lineColor = kDefaultLineColor;
    self.lineWidth = kDefaultLineWidth;
    self.lineAlpha = kDefaultLineAlpha;

    // set the transparent background
    // set to white instead... 
    // https://github.com/acerbetti/ACEDrawingView/issues/8

    self.backgroundColor = [UIColor whiteColor];

    self.originalFrameYPos = self.frame.origin.y;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)updateCacheImage:(BOOL)redraw
{
    // init a context
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);

    // draw white rectangle to use as background as workaround for
    // https://github.com/acerbetti/ACEDrawingView/issues/8

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(context, self.bounds);

    if (redraw) {
        // erase the previous image
        self.image = nil;

        // load previous image (if returning to screen)
        [[self.prev_image copy] drawInRect:self.bounds];

        // I need to redraw all the lines
        for (id<ACEDrawingTool> tool in self.pathArray) {
            [tool draw];
        }
    } else {
        // set the draw point
        [self.image drawAtPoint:CGPointZero];
        [self.currentTool draw];
    }
    // store the image
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

xiao99xiao commented 9 years ago

@0xPr0xy 's code wouldn't solve it when the drawingView load a UIImage as background, Just to make it clear.

Alisha786 commented 8 years ago

In My project i can change the background color when i am drawing,any body resolve this issue?? please explain