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

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

问题描述

所以,正如 this answeriOS 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: 内,如果你已经通过 创建了你自己的上下文,你只能使用 UIKit/UIGraphics 的东西进行绘制UIGraphicsBeginImageContext()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?

附:我知道我可以使用 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.

我相信 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天全站免登陆