title: How to draw a line with a custom style using Core Graphics?
body: <p>I'm currently drawing a line using Core Graphics. It's really bare bones and simple. </p>
  
  <pre><code>- (void)drawRect:(CGRect)rect {
      CGContextRef c = UIGraphicsGetCurrentContext();
      CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
      CGContextSetStrokeColor(c, red);
      CGContextBeginPath(c);
      CGContextMoveToPoint(c, 5.0f, 5.0f);
      CGContextAddLineToPoint(c, 300.0f, 600.0f);
      CGContextSetLineWidth(c, 25);
      CGContextSetLineCap(c, kCGLineCapRound);
      CGContextStrokePath(c);
  }
  </code></pre>
  
  <p>This works well. Let's say that we wanted to draw a custom style line. Say we wanted to imitate the style of a crayon for example. And that the designer handed your crayon style images: <a href="http://imgur.com/a/N40ig" rel="nofollow">http://imgur.com/a/N40ig</a> </p>
  
  <p>To do accomplish this effect I think I need to do something like this:</p>
  
  <ol>
  <li><p>Create a special colored versions of crayonImage1-crayonImage4</p></li>
  <li><p>Every time you add a line to line you use one of the crayonImages</p></li>
  <li><p>You alternate the crayonImages every time you draw a point.</p></li>
  </ol>
  
  <p>Step 1 makes sense. I can use the following method: </p>
  
  <pre><code>- (UIImage *)image:(UIImage *)img withColor:(UIColor *)color {    
      // begin a new image context, to draw our colored image onto
      UIGraphicsBeginImageContext(img.size);
  
      // get a reference to that context we created
      CGContextRef context = UIGraphicsGetCurrentContext();
  
      // set the fill color
      [color setFill];
  
      // translate/flip the graphics context (for transforming from CG* coords to UI* coords
      CGContextTranslateCTM(context, 0, img.size.height);
      CGContextScaleCTM(context, 1.0, -1.0);
  
      // set the blend mode to color burn, and the original image
      CGContextSetBlendMode(context, kCGBlendModeColorBurn);
      CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
      CGContextDrawImage(context, rect, img.CGImage);
  
      // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
      CGContextClipToMask(context, rect, img.CGImage);
      CGContextAddRect(context, rect);
      CGContextDrawPath(context,kCGPathFill);
  
      // generate a new UIImage from the graphics context we drew onto
      UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
  
      //return the color-burned image
      return coloredImg;
  }
  </code></pre>
  
  <p>I'm unsure how I can complete steps 2 and 3. Is there an API in CoreGraphics for setting an image as the point of line? If so what is it and how can I use it?</p>
  
  <p>Thanks in advance,</p>
  
  <p>-David</p>