2

I like to estimate the size an UIWebView is going to take, as I use the webview in a table and if I know the estimated height I can resize the tablecell to the correct height straight away.

So when I get the HTML, I planned to fire a low priority background threat to create the WebViews, calculate the size, and store this in the database with the HTML. Then when the user opens the article the height is available.

But when I do it this way I get the following error:

bool _WebTryThreadLock(bool), 0xb59b610: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

Is there a way to determine the height of the HTML without causing this problem?

Luuk D. Jansen
  • 4,402
  • 8
  • 47
  • 90

1 Answers1

1

You can make the UIWebView not visible, but it has to be on the main thread.

Try:

webView.layer.zPosition = -1.0;
webView.userInteractionEnabled = NO;
tillerstarr
  • 2,616
  • 1
  • 21
  • 35
  • I will try that, but am afraid that this will lock the main thread. I also have the problem that while it was in a seperate thread I could have the thread wait until all the info is available (check every 0.5 seconds) but cannot suspend the main thread like that and have to come up with a way of triggering the event. – Luuk D. Jansen Nov 10 '12 at 14:07
  • UIWebView is an unkind beast. I don't envy you the task; here is my similar question on knowing when the UIWebView is done rendering: http://stackoverflow.com/questions/5809212/how-do-i-tell-when-a-uiwebview-is-done-rendering-not-loading – tillerstarr Nov 10 '12 at 14:13
  • Well, I haven't looked at the permanence issue, but it kind of works, apart from the fact that the calculation is completely wrong (gives a very low height, not in line at all when loaded through the XIB when actually used. So it is still a bit of a puzzle. – Luuk D. Jansen Nov 10 '12 at 16:04
  • Does the frame match what it is when its loaded via the XIB? – tillerstarr Nov 10 '12 at 16:06
  • Yes, I created the webview with a frame of the same size. But it returns a very small required size (e.g. when loading in UI size 1045, where when by code 45). – Luuk D. Jansen Nov 10 '12 at 16:32
  • Never mind, made a small mistake myself, your solution works! Now test the performance! – Luuk D. Jansen Nov 10 '12 at 17:09