tags:

views:

147

answers:

1

Hi All.

I'm learning Cocoa Drawing techniques. I am creating two NSBezierPath objects from NSRects in my drawRect method, one which never changes, and one which changes width depending on something happening.

So in all likelyhood, my drawRect gets called numerous times, but it seems wrong to recreate the two NSBezierPath objects each time, it would be better to just update the second NSBezierPath width (or update its NSRect).

Can I do this? I tried to create the first NSBezierPath in the initWithFrame method, but it seemed to cause an issue.

+1  A: 

Have the view create both Bézier paths in its initWithFrame: method, and (if you're not using GC) release them in dealloc.

For the flexibly-sized path, drawRect: should figure out the correct width and gather the path's current width from the path's bounds, then use an affine transformation to scale the path by the appropriate factor. This will allow you to re-use the same two path objects for the entire lifetime of the view.

That said, as the proverb goes, premature optimization is the root of all evil. You shouldn't worry about this until you've found the app to be slow and run it under Shark or Instruments and proven that these objects' high turnover is a real problem. Let clear code be your priority until otherwise necessary.

Peter Hosey