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 ie
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 ie
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
-> I do this 4 times ie 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.
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.