This two-day project, because of the actual situation, you need to get the HTML string to load out, and the address of all the IMG tag plus host
At the beginning, I used the system's own approach
1 nsurl *baseurl = [Nsurl urlwithstring:@ " host "]; 2 [WebView loadhtmlstring:htmlstr Baseurl:baseurl];
BaseURL will load the picture when the image is automatically added to the front of the address, but this writing has a problem, that is, the host for http://www.baidu.com, this does not matter, if the host is Http://www.baidu.com/sss, The picture can't be loaded, I can't find the reason (if you know, you can tell me)
Then, I try to use the JS method to solve
- (void) Webviewdidfinishload: (UIWebView *) web{intCount = [[WebView stringbyevaluatingjavascriptfromstring:@"Document.images.length"] Intvalue];//get the number of IMG tags for(intI=0; i<count; i++) {NSString*old = [WebView stringbyevaluatingjavascriptfromstring:[nsstring stringWithFormat:@"document.images[%d].src", I]];//Get Image AddressOld = [Old stringbyreplacingoccurrencesofstring:@"file://"Withstring:@""];//If the above baseurl is empty, the host that automatically added the picture is removedNSString *New= [NSString stringWithFormat:@"document.images[%d].src= '%@ '", I, [NSString stringWithFormat:@"%@%@", Test_host, old]];//stitching the host and picture addresses together[WebView stringbyevaluatingjavascriptfromstring:New];//Update Picture }}
WebView Replace picture address when loading HTML