简体   繁体   English

Swift:UIWebView加载旧内容

[英]Swift: UIWebView loading old content

My App has one ViewController , one UIWebView and 4 UIImageView 我的应用程序有一个ViewController ,一个UIWebView和4个UIImageView

The control flow in my app: 我的应用程序中的控制流:

-> Capture a Image from camera and store into UIImage variable, this happens in didFinishPickingMediaWithInfo function. ->从相机捕获图像并将其存储到UIImage变量中,这发生在didFinishPickingMediaWithInfo函数中。 Do some processing on the Image. 对图像进行一些处理。

-> From didFinishPickingMediaWithInfo function, load UIWebView with an inline html/css code ie - >从didFinishPickingMediaWithInfo功能,负载UIWebView具有内嵌HTML / CSS代码,即

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

-> After the above html is loaded into UIWebView, delegate function webViewDidFinishLoad is called. ->将上述html加载到UIWebView之后,将调用委托函数webViewDidFinishLoad。 From webViewDidFinishLoad, take a snapshot of whatever is loaded in UIWebView into a Image with the code below: 从webViewDidFinishLoad中,使用以下代码拍摄UIWebView中加载到Image中的任何内容的快照:

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. ->将捕获的图像存储到UIImage变量中。

-> 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 ie ->将另外一个不同的html / css代码加载到同一UIWebView中(我仍然在webViewDidFinishLoad函数中):html加载代码与上述相同,即

WebView.loadHTMLString(htmlString1, baseURL: nil)

-> webViewDidFinishLoad is called again because of new html code loaded in above step, and I do the same thing ie take a snapshot of the UIWebView content into UIImage variable and load new html pattern ->由于在上述步骤中加载了新的html代码,再次调用了webViewDidFinishLoad,我做同样的事情,即将UIWebView内容的快照捕获到UIImage变量中并加载新的html模式

-> I do this 4 times ie in the end I have 4 Images captured in 4 UIImage variables. ->我执行了4次,即最后我在4个UIImage变量中捕获了4张图像。 I load all these 4 images into the 4 UIImageView on my storyboard. 我将所有这4个图像加载到情节提要中的4个UIImageView中。

-> 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. ->有时,Image1与Image 2相同,有时所有Image都相同。 This happens randomly. 这是随机发生的。 I know for sure that all these 4 images should be unique. 我肯定知道所有这四个图像应该是唯一的。 Because I load different html code in each step. 因为我在每个步骤中加载了不同的html代码。

  • What Am I doing wrong in the sequence above? 我按上述顺序做错了什么?

A couple things you might try: 您可以尝试以下几种方法:

1) Don't reload webView in webViewDidFinishLoad. 1)不要在webViewDidFinishLoad中重新加载webView。 Instead wait until the next run loop on main thread (allowing iOS to fully finish). 相反,要等到主线程上的下一个运行循环(允许iOS完全完成)。 In objective C, my code would look like 在目标C中,我的代码看起来像

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. 2)我有问题,因为在调用webViewDidFinishLoad时未刷新webView。 I solved this by adjusting the webView frame. 我通过调整webView框架解决了这一问题。 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. 这是在iOS 6中返回的,所以我不知道它是否会有所改变或会影响您的工作。

[webView loadHTMLString: html baseURL: nil];

// Play with frame to fix refresh problem
CGRect frame = webView.frame;
webView.frame = CGRectZero;
webView.frame = frame;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM