在ios中加载html时如何在wkwebview中隐藏带有src的图像?

Posted

技术标签:

【中文标题】在ios中加载html时如何在wkwebview中隐藏带有src的图像?【英文标题】:How to hide image with src in wkwebview when loading html in ios? 【发布时间】:2015-07-18 07:19:25 【问题描述】:

我正在使用 WKWebView 加载 html 字符串,html 字符串的某些末尾有一些丑陋的图片链接,我想隐藏它们。

css用来隐藏图片,但不起作用。

.article img[src* = "/smilies/"],
.article img[src* = ".feedburner.com/~ff/"],
.article img[src* = ".feedburner.com/~r/"],
.article img[src* = ".feedblitz.com/"]

    display: none;

我想隐藏的带有 feedburner src 的示例 html 字符串:

<div>
<a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk" border="0"></a>
</div>

【问题讨论】:

【参考方案1】:

实现此目的的一种快速而肮脏的方法是使用正则表达式。请注意,这对于长 HTML 文件确实不理想,因为它不如真正的 HTML 解析器高效。

// The HTML you posted
NSString *HTML = @"<div>\n\t<a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:yIl2AUoC8zA\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:qj6IDK7rITs\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:I9og5sOYxJI\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk\" border=\"0\"></a>\n</div>";

// A string containing source of the images that you want to delete
NSString *source = @"http://feeds.feedburner.com/~ff/";

// Builds a pattern that matches the tags of the images you want to delete
NSString *pattern = [NSString stringWithFormat:@"<img src=\"%@.+?>", source];

// The actual delete operation
NSString *cleanHTML = [HTML stringByReplacingOccurrencesOfString:pattern
                                                      withString:@""
                                                         options:NSRegularExpressionSearch
                                                           range:NSMakeRange(0, HTML.length)];

// Do what you want with the cleaned HTML (display it, ...)
NSLog(@"%@", cleanHTML);

【讨论】:

以上是关于在ios中加载html时如何在wkwebview中隐藏带有src的图像?的主要内容,如果未能解决你的问题,请参考以下文章

来自html字符串的iOS Swift 4图像未在WKWebView中加载

iOS:如何在 webview 或 wkwebview 中加载动画 webp 图像

如何在 WKWebView 中加载的页面中的隐藏字段中获取值绑定

检测页面是不是在 JavaScript 中的 WKWebView 中加载

为啥我在 iOS13.2 中加载 WKWebView 时收到控制台警告:[Process] kill() 返回意外错误 1?

在 WKWebView 中加载本地文件在设备中不起作用