IOS UIwebView 加载网络图片 使用相对地址
2014-02-07 15:05
253 查看
方法一:
在html文件内直接使用file:///user//xx//image.png的绝对路径
注:这样可以显示图片,但是如果在程序目录修改,图片就不能显示
方法二:
在html使用占位符,如:在html内使用<image src="file:/[myimage]" height="200" width="320">
在加载html文本的时候,replace这个占位符
代码如下:
string htmlpath=Path.GetFullPath("Docs/Detail.html");//根据html的相对路径获取html文件的绝对路径
string htmltext=File.OpenText(htmlpath).ReadToEnd();//读取html的内容
string imagepath=Path.GetFullPath("Images/image.png").Replace("/","//").Replace(" ","%20");//读取图片的绝对路径后替换掉单斜杠和空格
htmltext=htmltext.Replace("[myimage]",imagepath);//将html内容内的image占位符替换成正确的路径
webview.LoadHtmlString(htmltext,null);//webview加载html内容
注:如果图片过多则处理很麻烦,只能做例子写写,不能真正使用
方法三:
使用webview.LoadHtmlString(htmltext,BaseUrl);//BaseUrl:sets the main page content and base Url
在方法三中,我们在html文件中图片地址只用使用相对地址了 <image src="Images/image.png" height="200" width="320">
代码:
NSUrl BaseUrl=new NSUrl(Path.GetFullPath("."),true);//获取程序的根目录路径
webview.LoadHtmlString(htmltext,BaseUrl);//使用此重载方法的时候,图片的路径或自动处理成BaseUrl+image.src,这样图片就能正常显示了
例如:
jsString = [NSStringstringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-size:%fpx; line-height:%fpx;background-color: transparent;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", fontSize ,line_height,htmlText];
NSURL *urlBai=[NSURLURLWithString:@"http://42.96.192.186"];
[showWebView loadHTMLString:jsString baseURL: urlBai];
注:在使用次方法的时候,html更容易处理,程序也容易处理
综述:在以上三种方法种,第三种方法最优
在html文件内直接使用file:///user//xx//image.png的绝对路径
注:这样可以显示图片,但是如果在程序目录修改,图片就不能显示
方法二:
在html使用占位符,如:在html内使用<image src="file:/[myimage]" height="200" width="320">
在加载html文本的时候,replace这个占位符
代码如下:
string htmlpath=Path.GetFullPath("Docs/Detail.html");//根据html的相对路径获取html文件的绝对路径
string htmltext=File.OpenText(htmlpath).ReadToEnd();//读取html的内容
string imagepath=Path.GetFullPath("Images/image.png").Replace("/","//").Replace(" ","%20");//读取图片的绝对路径后替换掉单斜杠和空格
htmltext=htmltext.Replace("[myimage]",imagepath);//将html内容内的image占位符替换成正确的路径
webview.LoadHtmlString(htmltext,null);//webview加载html内容
注:如果图片过多则处理很麻烦,只能做例子写写,不能真正使用
方法三:
使用webview.LoadHtmlString(htmltext,BaseUrl);//BaseUrl:sets the main page content and base Url
在方法三中,我们在html文件中图片地址只用使用相对地址了 <image src="Images/image.png" height="200" width="320">
代码:
NSUrl BaseUrl=new NSUrl(Path.GetFullPath("."),true);//获取程序的根目录路径
webview.LoadHtmlString(htmltext,BaseUrl);//使用此重载方法的时候,图片的路径或自动处理成BaseUrl+image.src,这样图片就能正常显示了
例如:
UIWebView *twoWebView =[[UIWebView alloc]initWithFrame:CGRectMake(0, 100, 200, 100)];
jsString = [NSStringstringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-size:%fpx; line-height:%fpx;background-color: transparent;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", fontSize ,line_height,htmlText];
NSURL *urlBai=[NSURLURLWithString:@"http://42.96.192.186"];
[showWebView loadHTMLString:jsString baseURL: urlBai];
[webView addSubview:twoWebView];
注:在使用次方法的时候,html更容易处理,程序也容易处理
综述:在以上三种方法种,第三种方法最优
相关文章推荐
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android网络图片加载缓存处理库的使用---第三方库学习笔记(五)
- 使用Volley加载网络图片
- 使用异步任务加载网络上的图片
- Android 使用开源库加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley入门到精通:使用Volley加载网络图片
- 使用MVP+Retrofit+RxJava 图片加载使用Fresco 做网络请求并展示数据
- Android Volley完全解析(二),使用Volley加载网络图片
- android 开发 - 网络图片加载库 Fresco 的使用。
- Android Volley完全解析(二),使用Volley加载网络图片
- Afinal加载网络图片及下载文件使用方法
- Android Volley完全解析(二),使用Volley加载网络图片
- Afinal加载网络图片及下载文件使用方法
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Glide使用 加载网络图片 圆形图片 圆角图片
- 自定义ImageView实现类似微信朋友圈发图片点击出来阴影效果;同时使用Picasso加载网络图片
- 使用自定义的item、Adapter和AsyncTask、第三方开源框架PullToRefresh联合使用实现自定义的下拉列表(从网络加载图片显示在item中的ImageView)