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

后端 未结 2 814
栀梦
栀梦 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 ;-)

    0 讨论(0)
  • 2020-12-04 04:50
    if(erase)
        {
            UITouch *touch = [touches anyObject];
            CGPoint currentTouch = [touch locationInView:extraImageVw];
    
            CGFloat brushSize;
            if (isEraser)
            {
                brushSize=25.0;
            }
            else
            {
                brushSize=25.0;
            }
            CGColorRef strokeColor = [UIColor whiteColor].CGColor;
    
            UIGraphicsBeginImageContext(extraImageVw.frame.size);
    
            CGContextRef context = UIGraphicsGetCurrentContext();
            [extraImageVw.image drawInRect:CGRectMake(0, 0, extraImageVw.frame.size.width, extraImageVw.frame.size.height)];
            CGContextSetLineCap(context, kCGLineCapRound);
            CGContextSetLineWidth(context, brushSize);
            if (isEraser) {
                CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:fullPathToFile]].CGColor);
            }
            else
            {
                CGContextSetStrokeColorWithColor(context, strokeColor);
                CGContextSetBlendMode(context, kCGBlendModeClear);
            }
            CGContextSaveGState(UIGraphicsGetCurrentContext());
            CGContextBeginPath(context);
            CGContextMoveToPoint(context, Lastpoint.x, Lastpoint.y);
            CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
            CGContextStrokePath(context);
            extraImageVw.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
    
            Lastpoint = [touch locationInView:extraImageVw];
    
    
        }
    
    0 讨论(0)
提交回复
热议问题