Image Display when monotouch loads a local HTML file when using uiwebview

Source: Internet
Author: User

Method 1:

Use the absolute path file: // user // XX // image.png in the HTML file.

Note: This will display the image, but ifProgramAfter the directory is modified, the image cannot be displayed.

Method 2:

Use placeholders in HTML, for example, <image src = "file:/[myimage]" Height = "200" width = "320">

Replace this placeholder when loading HTML text

 CodeAs follows:

String htmlpath = path. getfullpath ("docs/detail.html"); // obtain the absolute path of the HTML file based on the relative path of HTML.

String htmltext = file. opentext (htmlpath). readtoend (); // read HTML content

String ImagePath = path. getfullpath ("images/image.png "). replace ("/","//"). replace ("", "% 20"); // read the absolute path of the image and replace the single slash and space.

Htmltext = htmltext. Replace ("[myimage]", ImagePath); // Replace the image placeholder in the HTML content with the correct path.

Webview. loadhtmlstring (htmltext, null); // webview loads HTML content

Note: If there are too many images, processing is very troublesome. You can only write examples and cannot use them.

Method 3:

Use webview. loadhtmlstring (htmltext,Baseurl); // Baseurl: sets the main page content and base URL

In method 3, we only use relative addresses for image addresses in HTML files.

<Image src = "images/image.png" Height = "200" width = "320">

Code:

Nsurl baseurl = new nsurl (path. getfullpath ("."), true); // obtain the root directory path of the program



Webview. loadhtmlstring (htmltext, baseurl); // when this overload method is used, the image path is automatically processed as baseurl + image. SRC, so that the image can be displayed normally.

Note: When using the second method, HTML is easier to process and the program is easier to process.

Summary: The three methods and the third method are optimal.

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.