Drawing custom graphics on the iPhone: CALayer vs. CGContext

Posted by Henry Cooke on Stack Overflow See other posts from Stack Overflow or by Henry Cooke
Published on 2010-06-09T02:31:05Z Indexed on 2010/06/09 2:32 UTC
Read the original article Hit count: 983

Hi all,

I have an application in which I'm doing some custom drawing, a bunch of lines on a gradient background, like so (ignore the text, they're just UILabels):

At the moment, that's all done by starting a new CGContext, drawing stuff into it with CGContextDrawLinearGradient and CGContextStrokePath, then finally saving the resulting image with UIGraphicsGetImageFromCurrentImageContext. The positioning info is calculated while I'm laying out those labels, so it'd be a PITA (and duplication of effort) to calculate it all over again when the containing UIView is drawn with drawRect, so I'm drawing it ahead of time into a UIImage. All works fine, so far so good.

However, I have a sneaking suspicion that it may be more efficient to use CALayers to do this drawing. My (cursory) understanding of the difference between the two approaches is that a CALayer is more like a bunch of instructions to draw stuff, and so takes up less memory until it's actually drawn onscreen, whereas drawing everything into a UIImage ahead of time means that you've got a sodding great bitmap kicking around in memory all the time, whether it's drawn or not.

Is that a correct understanding? What is generally considered to be the best way of drawing custom images on the iPhone?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about drawing