title: Optimization of drawing lines, possible alternatives to CAShapeLayer
body: <p>I need to draw many lines (in the range of 50-75) in a screen and currently use the below function for it which works fine. After drawing 40-50 of these lines with the below code, the application slows down noticeably in my iPhone 4. To optimize I tried removing the line shadow it helped but still app wasn't running as smooth as I wanted. I need to optimize the below code, my first idea is to replace the cashapelayers with .png line images. But the new method should support line rotation, different length line with same width, and animation of drawing (it seems a lot for me to do with cgaffinetransforms). Any ideas that can help me?</p>
  
  <pre><code>+ (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;
  }
  </code></pre>
  
  <p><strong>PARTLY SOLVED</strong> (Optimization never ends)</p>
  
  <p>I broke down my line drawing function into 2 complementary parts and draw multiple lines into the one shape layer instead of creating new layers each time. It works much better if not great. Here is the updated code:</p>
  
  <pre><code>+ (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);
  }
  </code></pre>