2014-12-01 33 views
0

我正在一個藍牙LE繪圖應用程序。當我在第一個設備上繪製一條線時,我就擁有了它,在各個點之間繪製了一條貝塞爾曲線。並且我有應用程序將這些點的各種x和y位置發送到接收設備,並且我可以將線條繪製在接收設備上。不過,我希望接收圖形是曲線,因爲點之間的直線是直的。bezier曲線在藍牙繪圖應用程序xcode 6

這是我的代碼在發送設備上使用bezier曲線繪製線條。

- (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]; 
    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; 

這是我使用的代碼來繪製點之間的線 我已經嘗試使用相同的代碼來繪製上面的貝塞爾曲線,但我最終從(0,0)輻射到各個點的線和我嘗試將ctr值發送到接收設備,然後將它們插入公式中,但根據設備1上的原始形狀繪製速度或速度有多快,我得到了一些非常奇怪的線條。任何想法都將不勝感激。

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

    } 
} 

回答

0

我明白了這一點,請參閱原文,瞭解我是如何做到的。