Better late than never
This thread helped me solve a problem in the application I am currently developing. I have a more efficient solution thanks to you guys. I'm sure it's too late to be useful for anyone in this thread but it may be helpful to other people in the future. Part of my application is a drawing canvas w/ any number of backgrounds that can be loaded, including plain colors, notepaper, images, pdf's, etc. I have a layer over the top of the background that all of the drawing gets done on. Now thanks to this thread I am using
CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeClear);
for all the drawing. The drawing is fairly standard code that goes like this:
//------------------------------------------------------------------------
pCoordY = [[previousArray objectAtIndex:2] floatValue];
pCoordX = [[previousArray objectAtIndex:1] floatValue];
nCoordY = [[currentArray objectAtIndex:2] floatValue];
nCoordX = [[currentArray objectAtIndex:1] floatValue];
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeNormal);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext( ), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentCon text(), redColor, greenColor, blueColor, alphaColor);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext() , pCoordX, pCoordY);
CGContextAddLineToPoint(UIGraphicsGetCurrentContex t(), nCoordX, nCoordY);
CGContextStrokePath(UIGraphicsGetCurrentContext()) ;
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//-----------------------------------------------------------------------
To make a circular eraser tool, you need only make two changes:
From:
CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeNormal);
To:
CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeClear);
And:
From:
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentCon text(), redColor, greenColor, blueColor, alphaColor);
To:
CGContextSetStrokeColorWithColor(UIGraphicsGetCurr entContext(), [UIColor clearColor].CGColor);
I'm sure everybody's app has some particulars so that copy/pasting this code will be difficult. But just the same this is how I accomplished it and it works fabulously well for my app.
Best of luck!
|