我用Quartz-2D表示iPhone在地图上显示路线。这条路线是按温度着色的。因为有些街道是黄色的,所以我在路线下用一条稍厚的黑线来创造一个边界效应,这样黄色的部分就可以在黄色的街道上出现。但是,即使黑线和路线一样厚,整个路线看起来就像一条蠕虫(非常丑陋)。我认为这是因为我从一个路线点画到另一个路线点,而不是使用最后一个路线点作为下一个起点。这样,如果有两个路径点丢失,路线将仍然没有削减。
要在没有蠕虫效应的情况下显示两行代码,我需要做什么?
-(void) drawRect:(CGRect) rect
{
CSRouteAnnotation* routeAnnotation = (CSRouteAnnotation*)self.routeView.annotation;
// only draw our lines if we're not int he moddie of a transition and we
// acutally have some points to draw.
if(!self.hidden && nil != routeAnnotation.points && routeAnnotation.points.count > 0)
{
CGContextRef context = UIGraphicsGetCurrentContext();
Waypoint* fromWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:0]];
Waypoint* toWaypoint;
for(int idx = 1; idx < routeAnnotation.points.count; idx++)
{
toWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:idx]];
CLLocation* fromLocation = [fromWaypoint getLocation];
CGPoint fromPoint = [self.routeView.mapView convertCoordinate:fromLocation.coordinate toPointToView:self];
CLLocation* toLocation = [toWaypoint getLocation];
CGPoint toPoint = [self.routeView.mapView convertCoordinate:toLocation.coordinate toPointToView:self];
routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor];
CGContextBeginPath(context);
CGContextSetLineWidth(context, 3.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGContextClosePath(context);
CGContextBeginPath(context);
CGContextSetLineWidth(context, 3.0);
CGContextSetStrokeColorWithColor(context, routeAnnotation.lineColor.CGColor);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGContextClosePath(context);
fromWaypoint = toWaypoint;
}
[fromWaypoint release];
[toWaypoint release];
}
}另外,我得到了一个
<Error>: CGContextClosePath: no current point.错误,我认为这是胡扯。
请提醒我!)
发布于 2010-01-14 01:31:42
听起来可能和线头有关。试着用平底帽作图,看看它有帮助。
更新:我知道现在发生了什么。这是简单的“背景”线重叠的像素覆盖在前面的“背景”+“为地面”线绘制。您更改绘制所有“背景”线,然后所有“前台”线避免此问题。你的线条仍然透支一些像素,但只有相同的颜色,所以你不会注意到。(请注意,大多数线头帽样式都超出了直线中的实际点,从而使效果更加明显--更多重叠像素。)
发布于 2010-01-14 01:48:43
不要画两条线,你能不能画一条,然后用阴影能力
发布于 2010-01-14 20:50:35
谢谢你们!因此,简单地添加一个阴影或线头没有帮助,效果保持不变。然而,我只是稍微玩了一下代码,结果发现,如果我在完全路径中分别绘制两条线(在两个循环中),那么效果就消失了!然后,我在背景线上加了正方形的大写,使它稍微好一些。
现在我唯一想知道的是.这是怎么解释的?这是新代码,我稍后会优化它..。
-(void) drawRect:(CGRect) rect {
CSRouteAnnotation* routeAnnotation = (CSRouteAnnotation*)self.routeView.annotation;
// only draw our lines if we're not int he moddie of a transition and we
// acutally have some points to draw.
if(!self.hidden && nil != routeAnnotation.points && routeAnnotation.points.count > 0)
{
CGContextRef context = UIGraphicsGetCurrentContext();
Waypoint* fromWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:0]];
Waypoint* toWaypoint;
for(int idx = 1; idx < routeAnnotation.points.count; idx++)
{
toWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:idx]];
CLLocation* fromLocation = [fromWaypoint getLocation];
CGPoint fromPoint = [self.routeView.mapView convertCoordinate:fromLocation.coordinate toPointToView:self];
CLLocation* toLocation = [toWaypoint getLocation];
CGPoint toPoint = [self.routeView.mapView convertCoordinate:toLocation.coordinate toPointToView:self];
routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor];
CGContextBeginPath(context);
CGContextSetLineWidth(context, 3.5);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGContextClosePath(context);
fromWaypoint = toWaypoint;
}
// zweite for schleife
for(int idx = 1; idx < routeAnnotation.points.count; idx++)
{
toWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:idx]];
CLLocation* fromLocation = [fromWaypoint getLocation];
CGPoint fromPoint = [self.routeView.mapView convertCoordinate:fromLocation.coordinate toPointToView:self];
CLLocation* toLocation = [toWaypoint getLocation];
CGPoint toPoint = [self.routeView.mapView convertCoordinate:toLocation.coordinate toPointToView:self];
routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor];
CGContextBeginPath(context);
CGContextSetLineWidth(context, 3.0);
CGContextSetStrokeColorWithColor(context, routeAnnotation.lineColor.CGColor);
CGContextSetLineCap(context, kCGLineCapSquare );
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGContextClosePath(context);
}
[fromWaypoint release];
}
} https://stackoverflow.com/questions/2061441
复制相似问题