Reputation: 173
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?
Upvotes: 0
Views: 1494
Reputation: 28740
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:>];
if (!gt) {
[theScanner scanUpToString:@">" intoString:>];
}
if (gt) {
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",gt] withString:@"=\"\""];
}
}
return html;
}
Upvotes: 0
Reputation: 173
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:>] ;
}
html = [html stringByReplacingOccurrencesOfString:[ NSString stringWithFormat:@"%@>", gt] withString:@""];
return html;
}
Upvotes: 3
Reputation: 6306
check that to apply filter on url scheme : Need to filter certain UIWebView requests and load them in different UIWebViews
Upvotes: 0
Reputation: 10978
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...
Upvotes: 0