Gilbert
Gilbert

Reputation: 868

Create a UIImage by rendering UIWebView on a background thread - iPhone

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.

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:

I'm pretty close to giving up.

Is there a savior among you people?

Upvotes: 9

Views: 4977

Answers (2)

user438350
user438350

Reputation: 1

I've been working on this, too.

In a thread I create a view hierarchy, loop until the web-view has finished loading content.

The webview I create is inside of a UIViewController's viewdidload-- I've tried doing

if ([NSThread isMainThread] == NO) {[self performselectorOnMainThread: @selector(viewDidLoad)return;)}

And I've done the same for dealloc'ing the webView.

But that didn't work.. I've only found that we avoid UIWebView exceptions UNTIL we hit the autorelease pool...

I'm using instruments to figure out why.

Here's my attack strategy... I'm going to perform the render operation on the main thread with an off-screen view, having a separate thread running some sort of queue to manage them. I'm worried about UI lag, so it'll have to be fairly efficient.

Upvotes: 0

lxt
lxt

Reputation: 31304

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.

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.

Upvotes: 4

Related Questions