0

我只是简单地使用铅笔画。但是我必须从粗略的绘图到平滑的绘图来展示它。所以我在touchesMoved和中使用不同的代码touchesEnded

代码如下:

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

但我的问题是:touchMovedtouchesEnded. 我该怎么办?

4

1 回答 1

2
YourImage.clearsContextBeforeDrawing = YES;

//或者

self.view.clearsContextBeforeDrawing = YES;

希望,这会帮助你..享受

于 2012-04-24T08:09:22.253 回答