【问题标题】:bezier curve in bluetooth drawing app xcode 6蓝牙绘图应用程序 xcode 6 中的贝塞尔曲线
【发布时间】:2014-12-01 18:53:38
【问题描述】:

我正在开发一款蓝牙 LE 绘图应用程序。我有它,所以当我在第一个设备上画一条线时,会在各个点之间画一条贝塞尔曲线。我让应用程序将这些点的各种 x 和 y 位置发送到接收设备,我可以在上面画线。但是我想让接收图弯曲,因为现在点之间的线是直的。

这是我在发送设备上使用贝塞尔曲线绘制线条的代码。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    ctr = 0;
    UITouch *touch = [touches anyObject];
    pts[0] = [touch locationInView:self.tempImage];
    lastPoint = [touch locationInView:tempImage];
}


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

   UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.tempImage];
    currentPoint = [touch locationInView:tempImage];
    ctr++;
    pts[ctr] = p;

    if (ctr == 4)
    {
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

        [self draw2];


        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }
     NSLog(@"ctr:%d",ctr);
    lastPoint = currentPoint;
}

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

    [path removeAllPoints];
    ctr = 0;

    UIGraphicsBeginImageContext(self.tempImage.frame.size);

    [self.imageView.image drawInRect:CGRectMake(0,0, self.imageView.frame.size.width, self.imageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempImage.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];

    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempImage.image = nil;
    UIGraphicsEndImageContext();

}

- (void)draw2
{
    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];

    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
    [path moveToPoint:pts[0]];
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

    [[UIColor blackColor] setStroke];
        [path setLineWidth:1.0];

    [path stroke];

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    //    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImage setAlpha:1.0];
   UIGraphicsEndImageContext();
    }
}

然后我用这段代码发送 x,y 坐标

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic
{

    ax =  lastPoint.x;
    ay = lastPoint.y;
xString = [NSString stringWithFormat:@"%f",ax/see];
    textView.text = xString;
    yString = [NSString stringWithFormat:@"%f",ay/see];
    textView3.text = yString;
NSString *stringOne = self.textView.text;
    NSString *stringTwo = [stringOne stringByAppendingString:@","];
    NSString *stringThree = [stringTwo stringByAppendingString:self.textView3.text];
self.dataToSend = [stringEleven dataUsingEncoding:NSUTF8StringEncoding];

    [self.peripheralManager updateValue:dataToSend forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];
self.sendDataIndex = 0;

    // Start sending
    [self sendData];
}

在接收设备上,我使用此代码获取 x,y 坐标

NSArray *coorArray = [stringFromData componentsSeparatedByString:@","];
    firstString = [coorArray objectAtIndex:0];
    secondString = [coorArray objectAtIndex:1];
 x = firstString.intValue;
    message2.text = secondString2;
    y = secondString.intValue;
CGPoint currentPoint2 =  CGPointMake(x, y) ;

这是我用来在点之间画线的代码 我尝试使用相同的代码来绘制上面的贝塞尔曲线,但我最终得到了从 (0,0) 辐射到各个点的线,我尝试将 ctr 值发送到接收设备,然后将它们插入公式,但我得到了一些非常奇怪的线条,具体取决于我在设备 1 上以原始形状绘制的速度或速度。任何想法将不胜感激。

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

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound      );
 else {
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);

        CGContextBeginPath(UIGraphicsGetCurrentContext());


        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), currentPoint2.x, lastPoint.y);
        CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y,20,20));
        CGContextStrokePath(UIGraphicsGetCurrentContext());

        NSLog(@"clearing");
    }

    CGContextBeginPath(UIGraphicsGetCurrentContext());

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint2.x, currentPoint2.y);

    CGContextStrokePath(UIGraphicsGetCurrentContext());


   imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    lastPoint  = currentPoint2;
    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(imageView.frame.size);
        [tempImage.image drawInRect:CGRectMake(0, 0,tempImage.frame.size.width, tempImage.frame.size.height)];
        [imageView.image drawInRect:CGRectMake(0, 0,imageView.frame.size.width, imageView.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        if (mode == DrawingModePen) {
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenBlack CGColor]);
        }
        else {
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [self.backgroundColor CGColor]);
        }
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        tempImage.image = nil;

        UIGraphicsEndImageContext();
    }
- (void) draw3
{

    NSLog(@"this is being called");


    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
 self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImage setAlpha:1.0];


    UIGraphicsEndImageContext();


}

【问题讨论】:

    标签: xcode6 bluetooth-lowenergy cgcontext uibezierpath


    【解决方案1】:

    我想通了

     NSArray *coorArray = [stringFromData componentsSeparatedByString:@"ᴃ"];
        firstString = [coorArray objectAtIndex:0];
        secondString = [coorArray objectAtIndex:1];
        thirdString = [coorArray objectAtIndex:2];
        secondString2 =[coorArray objectAtIndex:3];
         zoomStringIn = [coorArray objectAtIndex:4];
      firstString2 = [coorArray objectAtIndex:5];
       thirdString2 = [coorArray objectAtIndex:6];
     widthStringIn = [coorArray objectAtIndex:7];
        NSLog(@"firststring:%@",firstString);
        NSLog(@"secondString;%@",secondString);
       NSLog(@"thirdString;%@",thirdString);
        NSLog(@"secondString2;%@",secondString2);
        zoom = zoomStringIn.intValue;
        NSLog(@"zoom:%f",zoom);
        x = firstString.intValue*zoom;
        message2.text = secondString2;
        y = secondString.intValue*zoom;
    
        if ([thirdString isEqualToString:@"clear"]) {
            lastPoint = CGPointMake(x, y);
        }
        else {}
         CGPoint currentPoint2 =  CGPointMake(x, y) ;
    
        x2 = firstString2.intValue;
        y2 = thirdString2.intValue;
    
      self.pencilString = thirdString;
    

    这是我用来在点之间画线的代码

    if ([pencilString isEqualToString:@"clear"]) {
            ctr = 0;
            pts[0] = currentPoint2;
    
    
    
    
    
    
    }
    
    else {
        ctr++;
        pts[ctr] = currentPoint2;
    }
    
    
    NSLog(@"ctr:%d",ctr);
     NSLog(@"pts[ctr].x:%f",pts[ctr].x);
    
    NSLog(@"currentPoint.x:%f",currentPoint2.x);
    NSLog(@"currentPoint.y:%f",currentPoint2.y);
     self.RealImage.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
    
    self.imageView.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
    
    self.tempImage.frame = CGRectMake(14-(x2*minus),138-(y2*minus),740*zoom,850*zoom);
    
    
    
    
    
    if ([thirdString isEqualToString:@"black"]) {
        pencilString = @"black";
        [show setBackgroundImage: [UIImage imageNamed:@"black crayon.png"] forState:UIControlStateNormal];show.hidden  = NO;
        [show setTitle:@"Black" forState:UIControlStateNormal];
    }
    
    
    
    
    
    
            if (ctr == 4)
    
        {
            pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
            [path moveToPoint:pts[0]];
            [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
    
    
            [self draw3];
    
    
    
    
    
    
            // replace points and get ready to handle the next segment
            pts[0] = pts[3];
            pts[1] = pts[4];
            ctr = 1;
        }
    
    
    
    
    
    
    if ([pencilString isEqualToString:@"clear"]) {
    
    
    
        [path removeAllPoints];
        ctr = 0;
     }
    
    
        UIGraphicsBeginImageContext(self.tempImage.frame.size);
        [self.imageView.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
        [self.tempImage.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        self.tempImage.image = nil;
    
    
    
        UIGraphicsEndImageContext();
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     }
        - (void) draw3
        {
    
            if ([pencilString isEqualToString:@"clear2"]) {
    
            UIGraphicsBeginImageContext(imageView.frame.size);
            [[UIColor clearColor] setStroke];
            [imageView.image drawInRect:CGRectMake(0,0,imageView.frame.size.width, imageView.frame.size.height)];
    
    
    
    
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
    
    
    
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    
            CGContextStrokePath(UIGraphicsGetCurrentContext());
    
            [path setLineWidth:20.0];
    
    
            [path stroke];
    
    
            imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
        }
    
    
        else{
        NSLog(@"this is being called");
    
    
        UIGraphicsBeginImageContext(self.tempImage.frame.size);
        [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
    
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
    
    if ([pencilString isEqualToString:@"black"]) {
            [[UIColor blackColor] setStroke];
            [path setLineWidth:w2*zoom];
        }
    
        [path stroke];
    
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
    
        //    CGContextStrokePath(UIGraphicsGetCurrentContext());
        self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
        [self.tempImage setAlpha:1.0];
    
    
        UIGraphicsEndImageContext();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2014-09-29
      • 2013-08-21
      相关资源
      最近更新 更多