iOS function to create a path’s outline

Published by Petrakeas in Software

Screen-shot-2011-09-12-at-11

Υου’ll wonder why is that useful… Well for a number of reasons and for creating the shadowPath of a path!
If you want to render some paths with their shadow, you can set the CALayer‘s properties:

mycalayer.shadowOpacity = 0.8;

But if you don’t specify a shadowPath, the layer’s alpha channel will be used which is slow for real time changes. The solution is to specify a CGpath as the shadowPath. But apple doesn’t give you a function that creates a shadowPath around your path. So… I’ve written one!

The function accepts an open or closed  CGpath and the desirable width for the outline and returns the outline as a closed CGpath. It uses the Acclerate Framework for faster calculations.

I have created a demo that animates the outline’s width as well as the rotation of a path. The outline of one of the paths is used as a shadowPath. You can find the function inside this demo project here: https://github.com/Petrakeas/outlineDEMO

A video of the demo follows and some technical details.

UPDATE: I have added some more functions that help in the creation of bezier curves and updated the demo accordingly. Read the post here.

  • My function is a class method function so that it can be used without initializing a class instance first.
  • The animation in this demo is done using CABasicAnimation for custom properties. A very good guide for the topic is here. Generally you have to synthesize your properties, override “needsDisplayForKey:” , override “initWithLayer” in order to copy your custom variables and custom properties (because CA creates copies of your layer)
  • You can’t alter the shadowPath of an animated CAlayer,! So I created an external CAlayer for shadows and passed its reference to the animated CALayer so that it can change the shadowPath.
  • I use CGAffineTransform to rotate one of the paths and then created the outline of its path with my funtion.
  • The CAlayer sets its contentsScale in order to render properly in retina displays.

Comments (2)

Fastest way to draw shadows and blend layers in iOS at Wiggler
September 12th, 2011 at 10:20 pm

[...] for even faster shadows (for paths) create a shadowpath. Read more here. Related postsiOS function to create a path’s [...]

Petrakeas μpost (17 Jun 2012) at Wiggler
June 17th, 2012 at 1:19 pm

[...] have added these functions in my “outline calculation” functions. I have updated the demo to make use of both of them separately and in chain (calculate a smoothed [...]