How to erase the context I drawn on to the image in iphone sdk?
I am having an image on my view and i am painting on the image with colors like the code below:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2)
{
//imageDraw.image = nil;
return;
}
else
{
}
lastPoint = [touch locationInView:imageDraw];
lastPoint.y -= 5;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:mView];
currentPoint.y -= 5;
UIGraphicsBeginImageContext(imageDraw.frame.size);
[imageDraw.image drawInRect:CGRectMake(0, 0, imageDraw.frame.size.width, imageDraw.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 0.5);
DrawAppDelegate *appDelegate=(DrawAppDelegate*)[ [UIApplication sharedApplication] delegate];
UIColor *clr = appDelegate.txtColor;
[clr setStroke];
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imageDraw.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10)
{
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
DrawAppDelegate *appDelegate=(DrawAppDelegate*)[ [UIApplication sharedApplication] delegate];
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2)
{
//imageDraw.image = nil;
return;
}
else
{
}
if(!mouseSwiped)
{
UIGraphicsBeginImageContext(imageDraw.frame.size);
[imageDraw.image drawInRect:CGRectMake(0, 0, imageDraw.frame.size.width, imageDraw.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
UIColo开发者_运维问答r *clr = appDelegate.txtColor;
[clr setStroke];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
imageDraw.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
-(void)drawText
{
/*TextViewController *viewController = [[TextViewController alloc]init];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
*/
colorWheel = [[ColorPickerImageView alloc] initWithFrame:CGRectMake(20,20,300,340)];
[colorWheel setImage:[UIImage imageNamed:@"colorWheel1.png"]];
[colorWheel setUserInteractionEnabled:YES];
colorWheel.backgroundColor = [UIColor clearColor];
colorWheel.pickedColorDelegate = self;
[mView addSubview:colorWheel];
[self animateColorWheelToShow:YES duration:0.3];
}
- (void) pickedColor:(UIColor*)color
{
//mView.backgroundColor= color;
DrawAppDelegate *appDelegate=(DrawAppDelegate*)[ [UIApplication sharedApplication] delegate];
[self animateColorWheelToShow:NO duration:0.3];
appDelegate.txtColor = color;
//[self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)];
[mView setNeedsDisplay];
}
Now I want to erase some painted color on the image.Can anyone suggest me How I can I do any ideas pls with some sample code.
Anyone's help will be deeply appreciate.
Thanks to all, Monish.
The answer is in the order of the lines. To clear and erase use clearColor.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
self.previousPoint1 = [touch previousLocationInView:self.imageView];
self.previousPoint2 = [touch previousLocationInView:self.imageView];
self.currentPoint = [touch locationInView:self.imageView];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
self.previousPoint2 = self.previousPoint1;
self.previousPoint1 = [touch previousLocationInView:self.imageView];
self.currentPoint = [touch locationInView:self.imageView];
// calculate mid point
CGPoint mid1 = midPoint(self.previousPoint1, self.previousPoint2);
CGPoint mid2 = midPoint(self.currentPoint, self.previousPoint1);
UIGraphicsBeginImageContext(self.imageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)];
if (self.segmentedControl.selectedSegmentIndex == 11) {
CGContextSetBlendMode(context, kCGBlendModeClear);
} else {
CGContextSetBlendMode(context, kCGBlendModeNormal);
}
CGContextMoveToPoint(context, mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(context, self.previousPoint1.x, self.previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 20.0);
[self.color setStroke];
CGContextStrokePath(context);
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
- (NSArray *)colors {
return @[[UIColor colorWithRed:249/255.0 green:195/255.0 blue:207/255.0 alpha:1.0],
[UIColor colorWithRed:225/255.0 green:57 /255.0 blue:156/255.0 alpha:1.0],
[UIColor colorWithRed:97 /255.0 green:48 /255.0 blue:138/255.0 alpha:1.0],
[UIColor colorWithRed:255/255.0 green:199/255.0 blue:69 /255.0 alpha:1.0],
[UIColor colorWithRed:245/255.0 green:125/255.0 blue:55 /255.0 alpha:1.0],
[UIColor colorWithRed:194/255.0 green:25 /255.0 blue:48 /255.0 alpha:1.0],
[UIColor colorWithRed:135/255.0 green:214/255.0 blue:242/255.0 alpha:1.0],
[UIColor colorWithRed:23 /255.0 green:185/255.0 blue:181/255.0 alpha:1.0],
[UIColor colorWithRed:0 /255.0 green:77 /255.0 blue:119/255.0 alpha:1.0],
[UIColor colorWithRed:0 /255.0 green:104/255.0 blue:61 /255.0 alpha:1.0],
[UIColor colorWithRed:0 /255.0 green:163/255.0 blue:89 /255.0 alpha:1.0],
[UIColor clearColor],
[UIColor clearColor],
[UIColor clearColor],
[UIColor clearColor]
];
}
do like this..
for selecting somepart of yor image for erase
give this code,,
UIColor *selectedColor;
self.selectedColor=[UIColor whiteColor];
select that particular part with white color..
Thank u..
精彩评论