Moved CGContext setup code outside of a loop.