Want to add manual erasing option in ipad painting application by Quartz

后端 未结 2 826
栀梦
栀梦 2020-12-04 04:16

I am working on a painting application using Quartz 2D for ipad. Now I want to add an eraser option so that user can manually erase portion of his drawn line with touch.I ha

2条回答
  •  星月不相逢
    2020-12-04 04:37

    yes this is work well in my app ;-)

    firstly you add this code in touch began

    UITouch *touch = [touches anyObject];
            lastPoint = [touch locationInView:imgview];
            UIGraphicsBeginImageContext(imgview.frame.size);
            [imgview.image drawInRect:CGRectMake(0, 0, imgview.frame.size.width, imgview.frame.size.height)];
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:img].CGColor);
            CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor clearColor].CGColor);
            CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
            CGContextBeginPath(UIGraphicsGetCurrentContext());
    

    here img is ur background image then on move touch you simple write the line stroke code that code is this

    UITouch *touch = [touches anyObject];
            CGPoint currentPoint = [touch locationInView:imgview];
            NSLog(@"asdasdas");
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            imgview.image = UIGraphicsGetImageFromCurrentImageContext();
            lastPoint = currentPoint;
    

    then you see the final result is produce ;-)

提交回复
热议问题