Iphone 优化绘制线,可能的CAShapeLayer替代方案

Iphone 优化绘制线,可能的CAShapeLayer替代方案,iphone,ios,objective-c,optimization,cashapelayer,Iphone,Ios,Objective C,Optimization,Cashapelayer,我需要在一个屏幕上画很多线(在50-75范围内),目前使用下面的功能,效果很好。在使用下面的代码绘制了40-50行之后,我的iPhone4中的应用程序速度明显减慢。为了优化,我试着去除了线条阴影,这很有帮助,但应用程序仍然没有像我所希望的那样顺畅运行。我需要优化下面的代码,我的第一个想法是用.png行图像替换cashapelayers。但新方法应该支持线旋转、相同宽度的不同长度线和绘图动画(对我来说,这似乎与cgaffinetransforms有很多关系)。有什么能帮我的吗 + (CAShape

我需要在一个屏幕上画很多线(在50-75范围内),目前使用下面的功能,效果很好。在使用下面的代码绘制了40-50行之后,我的iPhone4中的应用程序速度明显减慢。为了优化,我试着去除了线条阴影,这很有帮助,但应用程序仍然没有像我所希望的那样顺畅运行。我需要优化下面的代码,我的第一个想法是用.png行图像替换cashapelayers。但新方法应该支持线旋转、相同宽度的不同长度线和绘图动画(对我来说,这似乎与cgaffinetransforms有很多关系)。有什么能帮我的吗

+ (CAShapeLayer *) drawLineOnView:(UIView *) view BetweenPoint1:(CGPoint) point1 Point2:(CGPoint) point2 lineWidth:(CGFloat)lineWidth lineColor:(UIColor *) color Animated:(BOOL) animed
{
    CAShapeLayer *lineShape = [CAShapeLayer layer];
    CGMutablePathRef linePath = nil;
    linePath = CGPathCreateMutable();

    //lineShape.opacity = 0.6;
    lineShape.lineWidth = lineWidth;
    lineShape.lineCap = kCALineCapRound;

    if(color==nil) color = [UIColor orangeColor]; //Default value
    lineShape.shadowColor = [color CGColor];
    lineShape.shadowOpacity = 1.0;
    lineShape.shadowRadius = 5.0;
    lineShape.strokeColor = [color CGColor];

    CGPathMoveToPoint(linePath, NULL, point1.x, point1.y);
    CGPathAddLineToPoint(linePath, NULL, point2.x, point2.y);

    if(animed)
    {
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 1.0;
        pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
        [lineShape addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
    }

    lineShape.path = linePath;
    CGPathRelease(linePath);
    [view.layer addSublayer:lineShape];

    return lineShape;
}
部分解决(优化永无止境)

我将我的线条绘制功能分解为两个互补部分,并在一个形状层中绘制多条线条,而不是每次创建新的层。如果不是很好,效果会更好。以下是更新的代码:

+ (CAShapeLayer *) createNewShapeLayerForDrawingLinesOnView:(UIView *) view lineWidth:(CGFloat)lineWidth lineColor:(UIColor *) color
{
    CAShapeLayer *lineShape = [CAShapeLayer layer];

    //lineShape.opacity = 0.6;
    lineShape.lineWidth = lineWidth;
    lineShape.lineCap = kCALineCapRound;

    if(color==nil) color = [UIColor orangeColor]; //Default value
    lineShape.shadowColor = [color CGColor];
    lineShape.shadowOpacity = 1.0;
    lineShape.shadowRadius = 5.0;
    lineShape.strokeColor = [color CGColor];

    [view.layer addSublayer:lineShape];
    return lineShape;
}

+ (void) addNewLineToShapeLayer:(CAShapeLayer *) shapeLayer BetweenPoint1:(CGPoint) point1 Point2:(CGPoint) point2
{
    CGMutablePathRef combinedPath = CGPathCreateMutableCopy(shapeLayer.path);

    CGMutablePathRef linePath = CGPathCreateMutable();

    CGPathMoveToPoint(linePath, NULL, point1.x, point1.y);
    CGPathAddLineToPoint(linePath, NULL, point2.x, point2.y);

    //No paths drawn before
    if(combinedPath == NULL)
    {
        combinedPath = linePath;
    }
    else
    {
        CGPathAddPath(combinedPath, NULL, linePath);
    }

    shapeLayer.path = combinedPath;
    CGPathRelease(linePath);
}

虽然我理解人们想要创建多个图层,但将所有线条绘制成一个图层,并从中管理线条列表的动画和旋转将更加高效。可以在具有组合路径的形状层中执行此操作,如(缺少标有“…”的代码):

甚至更快,您可以直接将线列表绘制到。视图的
drawRect:
方法的示例未经测试,但应能让您了解:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, lineWidth);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); //red

for(MyLine *line in lines)
{
    CGContextMoveToPoint(context, point1.x, point1.y);
    CGContextAddLineToPoint(context, point2.x, point2.y);
}

如果您需要进一步优化,您应该研究OpenGL。

您肯定不想要75层,每个层都有自己的线条。你确定不能在一个图层中绘制一条更复杂的路径吗?

我想在图形环境中绘制一条更优化的路径,正如你所说的,你能给出一个简单的伪代码或到followSure的链接吗。我会充实一点答案。不,我不想也不需要,我只是不知道如何做得更优化。在ne层中绘制所有内容?还是使用分析器?你没有给我们帮助你所需要的信息。
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, lineWidth);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); //red

for(MyLine *line in lines)
{
    CGContextMoveToPoint(context, point1.x, point1.y);
    CGContextAddLineToPoint(context, point2.x, point2.y);
}