简体   繁体   English

UIGraphicsGetCurrentContext():我们应该将CGContext作为函数参数

[英]UIGraphicsGetCurrentContext(): Should we put CGContext as function parameter

I'm using Core Graphic library for drawing on UIView under method drawRect . 我正在使用Core Graphic库在drawRect方法下的UIView上进行绘制。 As we know, under this method, we firstly get context by: 众所周知,在这种方法下,我们首先通过以下方式获取上下文:

- (void)drawRect:(CGRect)rect {
   CGContextRef context = UIGraphicsGetCurrentContext();
}

I often use helper method for drawing. 我经常使用辅助方法进行绘制。 So, in helper method, should I put this context as parameter. 因此,在辅助方法中,我应该将此上下文作为参数。 For example: 例如:

- drawCircle:(CGContextRef)context center:(int)center radius:(int)radius {
// drawing here
} 

Or we can get context variable later in helper function: 或者我们稍后可以在辅助函数中获取上下文变量:

- drawCircle: center:(int)center radius:(int)radius {
CGContextRef context = UIGraphicsGetCurrentContext();
// drawing here
}

I have tested and often don't see any problems. 我已经测试过了,经常看不到任何问题。 But I'm afraid, when I'm calling another method, something changes and UIGraphicsGetCurrentContext() will return different context variable. 但是恐怕,当我调用另一个方法时,会发生一些变化,并且UIGraphicsGetCurrentContext()将返回不同的上下文变量。 So is it safe to do in second way ? 那么第二种方式安全吗? Because it will make my code clearer. 因为它将使我的代码更清晰。 And if wrong, please tell me will be wrong under what circumstance ? 如果有错,请告诉我在什么情况下会错?

Thanks :) 谢谢 :)

Good question. 好问题。 I noticed in the documentation for UIGraphicsGetCurrentContext it says: 我在UIGraphicsGetCurrentContext文档中注意到:

The current graphics context is nil by default. 默认情况下,当前图形上下文为nil。 Prior to calling its drawRect: method, view objects push a valid context onto the stack, making it current. 在调用其drawRect:方法之前,视图对象将有效上下文推送到堆栈上,使其成为当前堆栈。 If you are not using a UIView object to do your drawing, however, you must push a valid context onto the stack manually using the UIGraphicsPushContext function. 但是,如果未使用UIView对象进行绘制,则必须使用UIGraphicsPushContext函数将有效上下文手动推入堆栈。

In iOS 4 and later, you may call this function from any thread of your app. 在iOS 4及更高版本中,您可以从应用程序的任何线程调用此函数。

It sounds like that last sentence is implying this function is thread safe, and generally it seems like with threads you're worried that if you access the same function or properties in different places you might be accessing different things or they might not be syncing correctly so you could possibly be getting different info back from two different places. 听起来好像最后一句话暗示该函数是线程安全的,并且通常来说,对于线程,您似乎担心,如果在不同位置访问相同的函数或属性,则可能会访问不同的内容,或者它们可能无法正确同步因此您可能会从两个不同的地方获取不同的信息。

I would assume that if it implies that this function is thread safe that it shouldn't matter if you access this from a different function as it should give you the same result, especially if you're not even using threads, so maybe this is more a matter of preference? 我会假设,如果它暗示此函数是线程安全的,则从其他函数访问此函数无关紧要,因为它应该给您相同的结果,尤其是在您甚至不使用线程的情况下,所以也许这是还有优先事项吗?

That said, in the 1st paragraph of that quote the second sentence mentions pushing a valid context onto the stack manually. 就是说,在该引用的第一段中,第二句提到手动将有效上下文推入堆栈。 If you code drawCircle where you pass in a context it seems like you have more flexibility because you can pass different contexts (if for some reason that was helpful) but if you hard code you can only ever use the default context returned by UIGraphicsGetCurrentContext(). 如果您在上下文中传递的地方编写drawCircle的代码,则似乎具有更大的灵活性,因为可以传递不同的上下文(如果出于某些原因,这很有用),但是如果您进行硬编码,则只能使用UIGraphicsGetCurrentContext()返回的默认上下文。 。

Here's a link to the docs: UIGraphicsGetCurrentContext Documentation 这是文档的链接: UIGraphicsGetCurrentContext文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM