通过在后台线程上渲染 UIWebView 创建 UIImage - iPhone

Create a UIImage by rendering UIWebView on a background thread - iPhone(通过在后台线程上渲染 UIWebView 创建 UIImage - iPhone)

本文介绍了通过在后台线程上渲染 UIWebView 创建 UIImage - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道一种方法,或者有关于如何获取从 UIWebView 呈现的 UIImage 的创意?关键是,它必须在后台线程上.

Does someone know of a way, or has a creative idea as to how to obtain an UIImage that is rendered from a UIWebView? The catch is, that is must be on a background thread.

我会详细说明:我试图每秒从多个 UIWebViews 获取图像+-,并将其显示在屏幕上(当然是 iPhone).因为将图层渲染到 CGContext 是一项消耗 CPU 的工作,所以我不想使用主线程,所以不要挂起 UI.

I'll elaborate: I'm trying to obtain the image from multiple UIWebViews every second+-, and display it on screen (iPhone's of course). Because rendering a layer to a CGContext is a CPU consuming job, I wouldn't like to use the main thread, so not to hang the UI.

到目前为止,我的尝试是:

My attempts so far were:

  • 我尝试在 webView 的层上使用 renderInContextUIGraphicalContext,但是 _WebTryThreadLock 错误导致 webview 崩溃.

  • I tried to use renderInContext on the webView's layer to the UIGraphicalContext, but the _WebTryThreadLock error crashed the webviews.

我尝试创建一个 CGBitmapContext,并将 webview 的图层渲染到它,但得到了相同的结果.

I tried creating a CGBitmapContext, and render the webview's layer to it, but got the same result.

我尝试实现一个 copy 方法(通过添加一个类别)到 CALayer,该方法深度复制了所有公共属性和子层.之后,我尝试 renderInContext 我复制的图层.我得到了一个部分正确"的 UIImage - 意思是,并非所有图层都被渲染,所以例如,我只会得到网站页眉(或页脚,或正文,或搜索栏,或只是一些框架).UIWebview 的层由各种子类 CALayers 组成,所以这可能就是这种方法不起作用的原因.

I tried implementing a copy method (by adding a Category) to CALayer, that deep copied all of the public properties, and sublayers. Afterwards, I tried to renderInContext the layer I copied. I got a UIImage that was partially "correct" - meaning, not all of the layers were rendered, so for example, i would get only the website header (or footer, or body, or search bar, or just a some of the frames). The UIWebview's layer consists of all sort of subclassed CALayers, so this is probably why this approached didn't work.

我尝试在 CATransaction 中设置 kCATransactionDisableActions,但似乎并没有改变这种行为(两者都没有).

I tried setting the kCATransactionDisableActions in a CATransaction, but it didn't appear to change this behavior (neither of them).

我已经快要放弃了.

你们中间有救世主吗?

推荐答案

UIWebView 讨厌,我的意思是 真的讨厌,在后台线程上对其进行任何处理.UIKit 不是完全线程安全的.绘制到图形上下文是(这是在 iOS 4 中添加的),但不是在辅助线程上创建 UIView.

UIWebView hates, and I mean really hates, having anything done to it on a background thread. UIKit is not fully thread safe. Drawing to a graphics context is (this was added in iOS 4), but not creating UIViews on a secondary thread.

你是在主线程之外创建你的 UIWebViews 吗?你也许有一些代码要分享?我怀疑您的问题是由于您尝试在辅助线程上对 UIWebView 执行操作这一事实引起的.将视图内容呈现为图像的绘图操作可以在主线程之外进行,但创建视图本身不能.

Are you creating your UIWebViews off the main thread? Do you perhaps have some code to share? I would suspect your issues are being caused by the fact you're trying to perform operations to a UIWebView on a secondary thread. The drawing operation to render the view's contents as an image can happen off the main thread, but creating the view itself can't.

这篇关于通过在后台线程上渲染 UIWebView 创建 UIImage - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过在后台线程上渲染 UIWebView 创建 UIImage - iPhone