iphonequartz-graphicscgcontextcgcontextdrawimage

clearContextBeforeDrawing in TouchEnd Method


I am using the pencil drawing simply. But i have to show it from rough drawing to Smooth drawing. So I am using the code differently in both touchesMoved and touchesEnded.

The codes are as follows:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];

    previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self.view];
    currentPoint = [touch locationInView:self.view];

    CGPathMoveToPoint(path, NULL, previousPoint1.x, previousPoint1.y);
    CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y);


    UIGraphicsBeginImageContext(imageview.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();


    [imageview.image drawInRect:CGRectMake(0, 0, imageview.frame.size.width, imageview.frame.size.height)];


    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineJoin(context, kCGLineJoinBevel);
    CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
     CGPathCloseSubpath(path);
    CGContextSetAllowsAntialiasing(context, YES);
     CGContextSetLineWidth(context, 2.0);  
    CGContextAddPath(context, path);
     CGContextStrokePath(context);

   imageview.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    CGPathRelease(path);
    path = CGPathCreateMutable();

    [self.PointArr addObject:[NSValue valueWithCGPoint:currentPoint]];
    [self.disArr addObject:[NSValue valueWithCGPoint:currentPoint]];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{


    UIGraphicsBeginImageContext(CGSizeMake(imageview.frame.size.width, imageview.frame.size.height));   
    [imageview.image drawInRect:CGRectMake(0, 0, imageview.frame.size.width, imageview.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());

    int curIndex = 0;
    CGFloat x0,y0,x1,y1,x2,y2,x3,y3;

    CGMutablePathRef pathh = CGPathCreateMutable();

    CGPathMoveToPoint(pathh,NULL,[[self.PointArr objectAtIndex:0] CGPointValue].x,[[self.PointArr objectAtIndex:0] CGPointValue].y);



    for(NSValue *v in self.PointArr){

        if(curIndex >= 4){
            for (int i=curIndex;i>=curIndex-4;i--) {
                int step = (curIndex-i);
                switch (step) {
                    case 0:
                        x3 = [(NSValue*)[self.PointArr objectAtIndex:i-1] CGPointValue].x;
                        y3 = [(NSValue*)[self.PointArr objectAtIndex:i-1] CGPointValue].y;  
                        break;
                    case 1:
                        x2 = [(NSValue*)[self.PointArr objectAtIndex:i-1] CGPointValue].x;
                        y2 = [(NSValue*)[self.PointArr objectAtIndex:i-1] CGPointValue].y;                      
                        break;
                    case 2:
                        x1 = [(NSValue*)[self.PointArr objectAtIndex:i-1] CGPointValue].x;
                        y1 = [(NSValue*)[self.PointArr objectAtIndex:i-1] CGPointValue].y;                      
                        break;
                    case 3:
                        x0 = [(NSValue*)[self.PointArr objectAtIndex:i-1] CGPointValue].x;
                        y0 = [(NSValue*)[self.PointArr objectAtIndex:i-1] CGPointValue].y;                      
                        break;  
                    default:
                        break;
                }           
            }


            double smooth_value = 0.5;

            double xc1 = (x0 + x1) / 2.0;
            double yc1 = (y0 + y1) / 2.0;
            double xc2 = (x1 + x2) / 2.0;
            double yc2 = (y1 + y2) / 2.0;
            double xc3 = (x2 + x3) / 2.0;
            double yc3 = (y2 + y3) / 2.0;

            double len1 = sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0));
            double len2 = sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
            double len3 = sqrt((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2));

            double k1 = len1 / (len1 + len2);
            double k2 = len2 / (len2 + len3);

            double xm1 = xc1 + (xc2 - xc1) * k1;
            double ym1 = yc1 + (yc2 - yc1) * k1;

            double xm2 = xc2 + (xc3 - xc2) * k2;
            double ym2 = yc2 + (yc3 - yc2) * k2;

            // Resulting control points. Here smooth_value is mentioned
            // above coefficient K whose value should be in range [0...1].
            double ctrl1_x = xm1 + (xc2 - xm1) * smooth_value + x1 - xm1;
            double ctrl1_y = ym1 + (yc2 - ym1) * smooth_value + y1 - ym1;

            double ctrl2_x = xm2 + (xc2 - xm2) * smooth_value + x2 - xm2;
            double ctrl2_y = ym2 + (yc2 - ym2) * smooth_value + y2 - ym2;   

            CGPathMoveToPoint(pathh,NULL,x1,y1);
            CGPathAddCurveToPoint(pathh,NULL,ctrl1_x,ctrl1_y,ctrl2_x,ctrl2_y, x2,y2);
            CGPathAddLineToPoint(pathh,NULL,x2,y2);
        }
        curIndex++;
    }
    CGContextAddPath(UIGraphicsGetCurrentContext(), pathh);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(),YES);
    imageview.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

But my problem is: the touchMoved context is not clear after drawing the same in touchesEnded. What should I do?


Solution

  • YourImage.clearsContextBeforeDrawing = YES;
    

    //Or

    self.view.clearsContextBeforeDrawing = YES;
    

    Hope, that will help you..enjoy