CoreGraphics Context Save and Restore Graphic State (helper) - ParkinT/RubyMotion_Life GitHub Wiki

Thanks to Mateus, in the RubyMotion Google Groups, for this.

Two years ago, I wrote some helpers for MacRuby CoreGraphics, you have to be careful when you're dealing with them, they're very expressive, wrapping them you lose the expressiveness and could be really difficult to debug when you have an issue. specially since we can not use instrument directly.

But you can reduce the Context state save and restore:

def CGContextSaveRestoreGState(&blk)
  context = UIGraphicsGetCurrentContext
  CGContextSaveGState(context)
  blk[context]
  CGContextRestoreGState(context)
end

CGContextSaveRestoreGState do |context|
  line_path = CGPathCreateMutable()
  line_pathY = 10.0
  CGPathMoveToPoint(linePath, nil, 1.0, line_pathY)
  CGPathAddLineToPoint(line_path, nil, self.bounds.size.width, line_pathY)
  CGContextAddPath(context, line_path)
  CGPathRelease(line_path)
  CGContextSetLineWidth(context, 1.0) CGContextSetStrokeColorWithColor(context, :black.uicolor(0.6).CGColor)
  GContextSetShadowWithColor(context, CGSizeMake(0.0, 1.0), 0.0, :white.u
  CGContextDrawPath(context, KCGPathStroke)
end 

It feels more naturally for me :-)