简体   繁体   中英

How can load an html page on iPhone webView without <img src=> tag

I load a html page in web view in this way:

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

I need to remove:

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

from NSString *description, to display the UIwebView without image and with only text. How can i do this?

I solved in this way:

- (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;
}  

I have modified the code and it's working perfectly. It will only remove src value. Remaining values and image tag are not modified

- (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;
}  

1/ load the HTML content in a string

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

2/ remove the <img /> tag in the string

3/ use loadHTMLString on you webView

Have a look at NSString reference

Instead of finding the <img /> tag in the HTML string, you can also perform a replace of <img by <img style='display:none;' to hide the image. But it will be still loaded...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM