0

My App has one ViewController, one UIWebView and 4 UIImageView

The control flow in my app:

-> Capture a Image from camera and store into UIImage variable, this happens in didFinishPickingMediaWithInfo function. Do some processing on the Image.

-> From didFinishPickingMediaWithInfo function, load UIWebView with an inline html/css code i.e.

webview.hidden = false
WebView.loadHTMLString(htmlString, baseURL: nil) 

-> After the above html is loaded into UIWebView, delegate function webViewDidFinishLoad is called. From webViewDidFinishLoad, take a snapshot of whatever is loaded in UIWebView into a Image with the code below:

var image:UIImage?
  autoreleasepool{
            UIGraphicsBeginImageContextWithOptions(WebView.frame.size, false, 1.0)
            WebView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
            image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
        }

-> Store the captured image into a UIImage variable.

-> Load one more different html/css code into the same UIWebView ( I am still in the webViewDidFinishLoad function): the html loading code is same as above i.e.

WebView.loadHTMLString(htmlString1, baseURL: nil)

-> webViewDidFinishLoad is called again because of new html code loaded in above step, and I do the same thing i.e. take a snapshot of the UIWebView content into UIImage variable and load new html pattern

-> I do this 4 times i.e. in the end I have 4 Images captured in 4 UIImage variables. I load all these 4 images into the 4 UIImageView on my storyboard.

-> Then I dismiss imagepicker

imagePicker.dismissViewControllerAnimated(true, completion: nil)

Here is the issue I am seeing in the end:

-> Sometimes, Image1 is same as Image 2, and sometimes all Images are the same. This happens randomly. I know for sure that all these 4 images should be unique. Because I load different html code in each step.

  • What Am I doing wrong in the sequence above?
Leo
  • 24,596
  • 11
  • 71
  • 92
  • You might want to clear the web view before loading another html http://stackoverflow.com/questions/6576154/how-to-clear-a-uiwebview – Shripada Oct 23 '15 at 04:39

1 Answers1

0

A couple things you might try:

1) Don't reload webView in webViewDidFinishLoad. Instead wait until the next run loop on main thread (allowing iOS to fully finish). In objective C, my code would look like

dispatch_async (dispatch_get_main_queue(), ^{
    // call method to load new html
});

2) I've had issues with webView not being refreshed by the time webViewDidFinishLoad was called. I solved this by adjusting the webView frame. Goofy, yes. This was back in iOS 6, so I have no idea if it makes any difference anymore or would affect what you are doing.

[webView loadHTMLString: html baseURL: nil];

// Play with frame to fix refresh problem
CGRect frame = webView.frame;
webView.frame = CGRectZero;
webView.frame = frame;
Ernie Thomason
  • 1,579
  • 17
  • 20
  • How do I wait for next run loop on main, this is what I did and it sill doesn't work: I called dispatch_async (dispatch_get_main_queue(), { from webViewDidFinishLoad.... that's what you were expecting me to do? – victor1111 Nov 21 '15 at 16:11
  • Yeah, that seems to be what I was suggesting you to try. You also might try my item #2. I'm not confident, but as I mentioned, it helped with a similar issue a few years back. – Ernie Thomason Nov 23 '15 at 05:54