UIKit绘图是线程安全的:你如何获得图形上下文? [英] UIKit drawing is thread safe: how do you get a graphics context?

查看:98
本文介绍了UIKit绘图是线程安全的:你如何获得图形上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,如此答案 iOS 4.0发行说明,UIKit现在有一些线程安全的绘图工具:

So, as mentioned in this answer and in the iOS 4.0 release notes, UIKit now has some thread-safe drawing facilities:


在UIKit中绘制图形上下文现在是线程安全的。具体来说:

Drawing to a graphics context in UIKit is now thread-safe. Specifically:


  • 用于访问和操作图形上下文的例程现在可以正确处理驻留在不同线程上的上下文。

  • 字符串和图像绘制现在是线程安全的。

  • 现在可以安全地使用多个线程中的颜色和字体对象。

这很好,但是你如何使用它?

That's great, but how do you use it?

据我所知,任何时候你不在里面 -drawRect:如果您通过 UIGraphicsBeginImageContext()或<$创建了自己的上下文,则只能使用UIKit / UIGraphics绘制内容。 c $ c> UIGraphicsPushContext(),但根据文档,这些函数不是线程安全的,并且总是调用 -drawRect()主线程。

As far as I'm aware, any time you're not inside -drawRect: you can only draw using the UIKit/UIGraphics stuff if you've created your own context through UIGraphicsBeginImageContext() or UIGraphicsPushContext(), but those functions are not thread safe according to the docs, and -drawRect() is always called on the main thread.

我认为在主线程上创建一个图像上下文然后启动后台方法将是一个非常糟糕的主意,因为竞争条件很多。

I assume that creating an image context on the main thread and then starting the background method would be a really bad idea, due to race conditions aplenty.

那么,我如何使用iOS 4中引入的基于多线程UIKit的绘图内容?我错过了哪些其他方式获得活跃的UIKit图形上下文?

So, how do I use this multithreaded UIKit-based drawing stuff that was introduced in iOS 4? What other ways of getting an active UIKit graphics context have I missed?

P.S。我知道我可以使用Core Graphics进行绘制并完成它。由于各种原因(遗留代码),我想继续使用基于UIKit的绘图方法。

P.S. I know that I could just draw using Core Graphics and be done with it. For various reasons (legacy code) I'd like to continue using the UIKit based drawing methods.

推荐答案

我的信念,基于尝试它并基于各种文档的一些经验是 UIGraphicsPushContext()的文档不正确。

My belief, based on some experience trying it and based on various documents, is that the docs for UIGraphicsPushContext() are incorrect.

I相信 UIGraphicsPushContext()实际上是线程安全的。这是真实的特殊迹象是 QA1637 ,其中写着从iOS 4.0开始在UIKit中绘制到图形上下文是线程安全的。这包括访问和操作当前图形堆栈,绘制图像和字符串,以及从辅助线程使用颜色和字体对象。 (强调我的)

I believe UIGraphicsPushContext() is in fact thread safe. The particular indication that this is true is QA1637, which says "Beginning with iOS 4.0, drawing to a graphics context in UIKit is thread-safe. This includes accessing and manipulating the current graphics stack, drawing images and strings, and usage of color and font objects from secondary threads." (emphasis mine)

我承认,假设线程安全与文档相矛盾,这总是一个冒险的主张。但我相信这是一个文档错误。我已经打开了 rdar:// 11161530 来跟踪它。请欺骗。

I acknowledge that it is always a dicey proposition to assume thread safety in contradiction to the docs. But I believe this is a documentation error. I have opened rdar://11161530 to track it. Please dupe.

这篇关于UIKit绘图是线程安全的:你如何获得图形上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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