调用didFinishNavigation时,WKWebView尚未完成加载-WKWebView中的错误? [英] WKWebView didn't finish loading, when didFinishNavigation is called - Bug in WKWebView?
问题描述
目标:在网站加载完成后拍摄WKWebView的屏幕截图
Goal: To take a screenshot of WKWebView after the website finished loading
采用的方法:
- 在UIViewController中定义WKWebView变量
-
创建了一个名为screen capture()的扩展方法,该方法获取WKWebView的图像
- Defined a WKWebView var in UIViewController
Created an extension method called screen capture() that takes image of WKWebView
使我的UIViewController实现WKNavigationDelegate
Made my UIViewController to implement WKNavigationDelegate
设置wkwebview.navigationDelegate = self(在UIViewController初始化中)
Set the wkwebview.navigationDelegate = self ( in the UIViewController init)
在UIViewcontroller中实现didFinishNavigation委托功能,以调用WKWebView的屏幕捕获扩展方法
Implemented the didFinishNavigation delegation func in UIViewcontroller to call screen capture extension method for WKWebView
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
let img = webView.screenCapture()
}
问题:
- 调试模拟器时,即使网站尚未在WKWebView中呈现,我仍注意到该控件到达了didFinishNavigation()函数.
- 相应地,所截取的图像截图为白色斑点.
我在这里想念什么?我查看了WKWebView的所有可能的委托函数,似乎没有其他东西代表WKWebView中内容加载的完成.如果能解决问题,将不胜感激
What am I missing here? I looked at all possible delegate functions for WKWebView and nothing else seem to represent the completion of content loading in WKWebView. Would appreciate help on if there is a work around
更新:添加我用来为网络视图拍摄屏幕截图的屏幕截图代码
Update: Adding screenshot code that I am using to take a screenshot for web view
class func captureEntireUIWebViewImage(webView: WKWebView) -> UIImage? {
var webViewFrame = webView.scrollView.frame
if (webView.scrollView.contentSize != CGSize(width: 0,height: 0)){
webView.scrollView.frame = CGRectMake(webViewFrame.origin.x, webViewFrame.origin.y, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height)
UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, webView.scrollView.opaque, 0)
webView.scrollView.layer.renderInContext(UIGraphicsGetCurrentContext())
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
webView.scrollView.frame = webViewFrame
return image
}
return nil
}
推荐答案
WKWebView不会使用委派来告知您内容加载完成的时间(这就是为什么找不到适合您目的的任何委托方法)的原因.知道WKWebView是否仍在加载的方法是使用KVO(键值观察)观察其loading
属性.这样,当loading
从true
更改为false
时,您会收到通知.
WKWebView doesn't use delegation to let you know when content loading is complete (that's why you can't find any delegate method that suits your purpose). The way to know whether a WKWebView is still loading is to use KVO (key-value observing) to watch its loading
property. In this way, you receive a notification when loading
changes from true
to false
.
这是一个循环的动画gif,显示了我测试时会发生的情况.我加载Web视图并通过KVO响应其loading
属性以拍摄快照.上面的视图是Web视图;下部(压缩的)视图是快照.如您所见,快照确实捕获了加载的内容:
Here's a looping animated gif showing what happens when I test this. I load a web view and respond to its loading
property through KVO to take a snapshot. The upper view is the web view; the lower (squashed) view is the snapshot. As you can see, the snapshot does capture the loaded content:
[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
if (self->_webKitView.isLoading == true) {
NSLog(@"Still loading...");
}else {
NSLog(@"Finished loading...");
[timer invalidate];
dispatch_async(dispatch_get_main_queue(), ^{
[self->_activityIndicator stopAnimating];
});
}
}];
这篇关于调用didFinishNavigation时,WKWebView尚未完成加载-WKWebView中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!