如何获得UIGraphicsPushContext的参数的上下文? [英] How to get context for the parameter of UIGraphicsPushContext?

查看:95
本文介绍了如何获得UIGraphicsPushContext的参数的上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如苹果的文档所说,UIGraphicsGetCurrentContext只能在drawRect方法中使用。
如果你想在另一个地方使用它,你必须推送一个上下文。

As the apple's document said, UIGraphicsGetCurrentContext can only be used in drawRect method. If you want to use it in another place, you have to push a context before.

现在我想使用UIGraphicsGetCurrentContext在我自己的方法称为render。
如何获取一个上下文?

Now I want to use UIGraphicsGetCurrentContext to get a context in my own method called render. How can I get a context to push?

我可以在drawRect中获取上下文并将其保存在非局部变量中吗?
并在另一个方法中推送,然后使用UIGraphicsGetCurrentContext来使用它。
如果是,为什么需要我推它,并再次得到它?我可以直接使用非本地变量。

Can I get the context in drawRect and save it in a non-local variable? And push it in another method, then use UIGraphicsGetCurrentContext to get it to use. If so, why need I push it and get it again? I can use the non-local variable directly.

推荐答案

可以调用 setNeedsDisplay 您需要在计时器上重绘,并让 drawRect 调用 render render 直接在计时器上)。这样你可以避免使用CG上下文进行不常见的操作,并且当矩形已经从屏幕上滚动时,可以防止呈现。

You can call setNeedsDisplay of the view that you need redrawn on timer, and have its drawRect call your render (instead of calling your render on timer directly). This way you would avoid unusual manipulations with your CG Context, and prevent rendering when the rectangle has been scrolled off the screen.

编辑:
使用UIGraphicsPushContext UIGraphicsPopContext当你想要一个特定的上下文成为UI Kit操作的上下文。我最初的理解他们做的是不正确的(我是相对新的iOS开发自己)。例如,存在对当前上下文隐式操作的操作(例如,设置颜色或其他绘制参数的一些操作)。如果你为一个位图设置一个上下文,例如绘制一个位图,然后你想使用修改当前上下文的状态的操作(即修改上下文参数,但不采用特定的上下文引用的操作一个参数),你推动位图上下文使其为当前,执行隐式引用它的操作,并立即弹出上下文。

You use UIGraphicsPushContext and UIGraphicsPopContext when you want a specific context to become the context on which UI Kit operates. My initial understanding of what they do was incorrect (I'm relatively new to iOS development myself). For example, there are operations (e.g. some operations setting a color or other drawing parameters) that operate implicitly on the current context. If you set up a context for, say, drawing on a bitmap, and then you want to use an operation that modifies the state of the current context (i.e. an operation that modifies the context parameters, but does not take a specific context reference as a parameter), you push the bitmap context to make it current, perform the operation that implicitly references it, and pop the context right back.

特别感谢rob mayoff以便向我解释这一点。

Special thanks go to rob mayoff for explaining this to me.

这篇关于如何获得UIGraphicsPushContext的参数的上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆