Ios 如何检测html中的图像是否已在WKWebView中完全加载?
我正在加载带有Ios 如何检测html中的图像是否已在WKWebView中完全加载?,ios,objective-c,Ios,Objective C,我正在加载带有标记的本地html,例如: < img class="icon" src="data:image/png;base64,/9j/4AAQSkZJRgABAQEAlgCWAAD/7gAOQWRvYmUAZAAAAAAA/... 更新: 我在didFinishNavigation委托中添加了一个延迟,图像加载非常完美: -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigati
< img class="icon" src="data:image/png;base64,/9j/4AAQSkZJRgABAQEAlgCWAAD/7gAOQWRvYmUAZAAAAAAA/...
更新:
我在didFinishNavigation委托中添加了一个延迟,图像加载非常完美:
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
if (webView.isLoading) return;
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSLog(@"Do some work");
});
}
然而,这是一个临时解决办法。有更好的解决方案吗?我的解决方案是使用javascript代码检查
webView
的内容高度。如果高度大于或等于一个数字(这意味着webView中有东西,它就是图像),请将消息发布到webView
并使用WKScriptMessageHandler
以本机代码处理消息
示例
创建javascript代码。检查内容的高度是否大于50,发送消息检查图像是否加载
NSString* source =
[NSString stringWithFormat:
@"var sizeInterval = setInterval("
"function() {"
"var h = 0;"
"var children = document.body.children;"
"for (var c = 0; c < children.length; c++) {"
"h += children[c].offsetHeight;"
"}"
"if (h > 50) {"
"clearInterval(sizeInterval);"
"window.webkit.messageHandlers.%@.postMessage('loaded');"
"}"
"},"
"100);",
kContentLoadedMessage];
WKUserScript* script = [[WKUserScript alloc]
initWithSource:source
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
WKUserContentController* contentController =
[[WKUserContentController alloc] init];
[contentController addUserScript:script];
[contentController addScriptMessageHandler:self name:kContentLoadedMessage];
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = contentController;
YOUR_WK_WEB_VIEW =
[[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
// Add webView to a view and load html
您可以查看更多信息。我的解决方案是使用javascript代码检查
webView
的内容高度。如果高度大于或等于一个数字(这意味着webView中有东西,它就是图像),请将消息发布到webView
并使用WKScriptMessageHandler
以本机代码处理消息
示例
创建javascript代码。检查内容的高度是否大于50,发送消息检查图像是否加载
NSString* source =
[NSString stringWithFormat:
@"var sizeInterval = setInterval("
"function() {"
"var h = 0;"
"var children = document.body.children;"
"for (var c = 0; c < children.length; c++) {"
"h += children[c].offsetHeight;"
"}"
"if (h > 50) {"
"clearInterval(sizeInterval);"
"window.webkit.messageHandlers.%@.postMessage('loaded');"
"}"
"},"
"100);",
kContentLoadedMessage];
WKUserScript* script = [[WKUserScript alloc]
initWithSource:source
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
WKUserContentController* contentController =
[[WKUserContentController alloc] init];
[contentController addUserScript:script];
[contentController addScriptMessageHandler:self name:kContentLoadedMessage];
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = contentController;
YOUR_WK_WEB_VIEW =
[[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
// Add webView to a view and load html
您可以查看更多了解。您可以参考以下帖子:是的,我已经在使用webView:didFinishNavigation:进行检测。但是,它是在加载base64映像后触发还是在加载之前触发?您可以参考以下帖子:是的,我已经在使用webView:didFinishNavigation:来检测。但是,它是在加载base64图像后触发还是在加载之前触发?Hi@trungduc,如果没有加载图像帧,这是一个很好的技巧。在我的例子中,图像帧就在那里(因此,加载图像和不加载图像时,网络视图的高度是相同的),只是图像没有加载:(好的,我知道了。你能告诉我你改变图像的方式吗?我想我们只需要改变javascript以适应你的问题。我只是使用一个简单的图像标记,数据源是base64格式,如问题所示。
- (void)userContentController:(WKUserContentController*)userContentController
didReceiveScriptMessage:(WKScriptMessage*)message {
if ([message.name isEqualToString:kContentLoadedMessage]) {
NSLog(@"Image was loaded");
}
}