How do I obtain the current graphics context for 2D drawing?

How do I obtain the current graphics context for 2D drawing?

You obtain the current context for 2D drawing from within your implementation of the drawRect: method for the UIView object that you plan to draw to, by calling the function UIGraphicsGetCurrentContext. This returns the current graphics context as an opaque type (CGContextRef).

You can also set the current context by calling the function UIGraphicsPushContext, passing the context you want to set. Then, you can restore a previous context by calling the method UIGraphicsPopContext. For every call to UIGraphicsPushContext, you must provide a balancing UIGraphicsPopContext call.

For examples, see the QuartzDemo sample code.

———————————————————————————————————————————————————————————————————–

我如何获取当前2D图形的图像内容?

你可以在准备绘制图像的UIView的对象中实现方法:drawRect,来获取当前2D图像的内容。方法中需要调用UIGraphicsGetCurrentContext函数。它将返回一个非透明类型(CGContextRef型)的图像内容。

你也可以通过调用函数:UIGraphicsPushContext,传递你想设置的内容,来设置当前图像的内容。然后你可以通过调用UIGraphicsPopContext来获得先前的图像内容。每调用一次UIGraphicsPushContext,你必须相应地调用一次UIGraphicsPopContext。

例子参看QuartzDemo代码样例。

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s