如何在没有<img src=""/>标记的iPhone webView上加载html页面

时间:2010-11-17 11:46:01

标签: iphone html uiwebview

我以这种方式在网页视图中加载一个html页面:

NSString *description = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.mypage.html"] encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:pRova baseURL:nil];

我需要删除:

<img src="http://www.mypage/image" align="left" style="padding: 0px 10px 0px 0px; width: 301px; height: 280px;" alt="diaspora-uys" />
从NSString *描述

,显示没有图像且只有文本的UIwebView。 我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:3)

我这样解决了:

- (NSString *)flattenHTML:(NSString *)html {
    NSScanner *theScanner;
    NSString *gt =nil;

    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<img" intoString:NULL] ; 

        // find end of tag
        [theScanner scanUpToString:@">" intoString:&gt] ;
    }

    html = [html stringByReplacingOccurrencesOfString:[ NSString stringWithFormat:@"%@>", gt] withString:@""];
    return html;
}  

答案 1 :(得分:0)

1 /将HTML内容加载到字符串

+(id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)

2 /删除字符串

中的<img />标记

3 /在webView上使用loadHTMLString

查看NSString reference

您还可以通过<img />执行<img替换来隐藏图像,而不是在HTML字符串中找到<img style='display:none;'标记。但它仍将被加载......

答案 2 :(得分:0)

检查是否在url方案上应用过滤器:     Need to filter certain UIWebView requests and load them in different UIWebViews

答案 3 :(得分:0)

我修改了代码并且它运行得很好。它只会删除src值。不修改剩余值和图像标记

- (NSString *)flattenHTML:(NSString *)html {
    NSScanner *theScanner;
    NSString *gt = nil;
    NSString *temp = nil;
    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<img" intoString:&temp]; 

        //find the src tag
        [theScanner scanUpToString:@"src" intoString:&temp]; 

        [theScanner scanUpToString:@"=" intoString:&temp];

        [theScanner scanUpToString:@" " intoString:&gt];
        if (!gt) {
            [theScanner scanUpToString:@">" intoString:&gt];    
        }

        if (gt) {
            html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",gt] withString:@"=\"\""];    
        }
    }

    return html;
}