2012-04-24 1 views
0

간단하게 연필 드로잉을 사용하고 있습니다. 하지만 거친 그림에서 부드러운 그림까지 보여줘야합니다. 그래서 나는 touchesMovedtouchesEnded에서 다르게 코드를 사용하고 있습니다.ClearContextBeforeDrawing in TouchEnd 메서드

-(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(); 
} 

을하지만 내 문제 : 다음 touchMoved 상황이 touchesEnded에서 같은 그리기 후 명확하지 않다 다음과 같이

코드는 있습니다. 어떻게해야합니까?

답변

2
YourImage.clearsContextBeforeDrawing = YES; 

// 또는

self.view.clearsContextBeforeDrawing = YES; 

희망, 즉 도움이 될 것입니다 you..enjoy