【发布时间】: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