使用Core Graphics / Cocoa,你能从后台线程中绘制位图上下文吗? [英] Using Core Graphics/ Cocoa, can you draw to a bitmap context from a background thread?

查看:370
本文介绍了使用Core Graphics / Cocoa,你能从后台线程中绘制位图上下文吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CGBitmapContextCreate 创建一个 CGContext ,然后生成 cgitmapContextCreateImage ,并在 drawRect 中绘制到我的视图中的CGImage m也绘制一些其他的东西在这 - 这是一个练习,隔离不同级别的变化和复杂性)。



这一切都工作正常,当它都运行在主线程。然而,以这种方式分裂这个事情的动机之一是,屏幕外部分可以在后台线程上运行(我认为应该确定,因为它不呈现到屏幕上下文)。



但是,当我这样做时,生成的图像是空的!我检查了代码,并放置明智的NSLog的验证一切都按正确的顺序发生。



我的下一步是煮这个最简单的代码重现问题(或找到一些愚蠢的东西,我错过了,并解决它) - 在这一点,我有一些代码,如果必要的话张贴在这里。但我首先想在这里检查,我不会走下错误的路径与这。我无法在我的旅行中找到任何东西,在googlesphere这两种方式流光 - 但一个朋友没有提到,他遇到了一个类似的问题,同时试图调整图像在后台线程 - 建议这里可能有一些一般的限制。 / p>



感谢您的回应。如果没有别的,他们告诉我,至少我不是一个人没有一个答案,这是我想要找出的一部分。在这一点上,我将把额外的工作,得到最简单的可能的例子,并可能回来一些代码或更多的信息。同时保留任何想法: - )



一点提示:有几个人使用了术语线程安全到API。应该注意,在这种情况下有两种类型的线程安全:


  1. API本身的线程性 - 即它可以用于所有来自多个线程(全局状态和其他重入问题,如C的strtok是一个API也许不是线程安全的常见原因)。

  2. 单个操作的原子性 - 可以多线程通过API与相同的对象和资源交互,而没有应用程序级锁定?

我怀疑提到到目前为止

[edit2 - 已解决!]



它一切工作。执行摘要是问题是与我,而不是位图上下文本身。



在我的背景线程,在我介绍位图上下文,我做一些准备对一些其他对象。事实证明,间接地,对那些其他对象的调用导致setNeedsDisplay在一些视图中被调用!
通过将那个部分分离到主线程,现在所有部分都可以完美地工作了。



因此,对于任何遇到这个问题的人来说,



感谢所有

$

b $ b

解决方案

只是猜测,但如果你试图从另一个线程调用setNeedsDisplay,你需要通过performSelectorOnMainThread调用它。


I'm drawing offscreen to a CGContext created using CGBitmapContextCreate, then later generating a CGImage from it with CGBitmapContextCreateImage and drawing that onto my view in drawRect (I'm also drawing some other stuff on top of that - this is an exercise in isolating different levels of variability and complexity).

This all works fine when it's all running on the main thread. However one of the motivations for splitting this out this way was so that the offscreen part could be run on a background thread (which I had thought should be ok since it's not rendering to an onscreen context).

However, when I do this the resulting image is empty! I've checked over the code, and placed judicious NSLog's to verify that everything is happening in the right order.

My next step is to boil this down to the simplest code that reproduces the issue (or find some silly thing I'm missing and fix it) - at which point I'd have some code to post here if necessary. But I first wanted to check here that I'm not going down the wrong path with this. I couldn't find anything in my travels around the googlesphere that sheds light either way - but a friend did mention that he ran into a similar issue while trying to resize images in a background thread - suggesting there may be some general limitation here.

[edit]

Thanks for the responses so far. If nothing else they have told me that at least I'm not alone in not having an answer for this - which was part of what I wanted to find out. At this point I'm going to put the extra work into getting the simplest possible example and may come back with some code or more information. In the meantime keep any ideas coming :-)

One point to bring up: A couple of people have used the term thread safety with respect to APIs. It should be noted that there are two types of thread safety in this context:

  1. Threadability of the API itself - ie can it be used at all from more than one thread (global state and other re-entrancy issues such as C's strtok are common reasons that an API might not be thread safe too).
  2. Atomicity of individual operations - can multiple threads interact with the same objects and resources through API without application level locking?

I suspect that mention so far has been of the first type, but would appreciate if you could clarify.

[edit2 - solved!]

Ok, I got it all working. Executive summary is that the problem was with me, rather than bitmap contexts themselves.

In my background thread, just before I drew into the bitmap context, I was doing some preparation on some other objects. It turns out that, indirectly, the calls to those other objects where leading to setNeedsDisplay being called on some views! By separating the part that did that out to the main thread it now all works perfectly.

So for anyone who hits this question wondering if they can draw to a bitmap context on a background thread, the answer is you can (with the caveats that have been presented here and in the answers).

Thanks all

解决方案

Just a guess, but if you are trying to call setNeedsDisplay from another thread, you need to call it via performSelectorOnMainThread instead.

这篇关于使用Core Graphics / Cocoa,你能从后台线程中绘制位图上下文吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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