WebView Replace picture address when loading HTML

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.