UIWebView如何加载本地图片

UIWebView加载本地图片是有实用价值的.比方说,有时候我们需要本地加载静态页来显示相关帮助信息,而这些帮助信息当中含有很多很多的富文本,用代码实现难度较大,这时候,可以考虑使用UIWebView加载本地图片的方式实现需求.

UIWebView是可以从本地加载图片的,只不过这个本地的图片需要是bundle中的图片

沙盒中的文件如何添加呢?

http://stackoverflow.com/questions/17494752/how-to-read-image-file-at-documents-directory-from-ios-webview

You could give the URL for this image using an absolute path with a file:// scheme.

NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)[];
NSString *filePath = [NSString stringWithFormat:@"file://%@/image.png", documentsDirectory];

You could then run some javascript to update the src tag to update it to the new path:

NSString *javascript = [NSString stringWithFormat:@"var imageElement = document.getElementById('localFile'); imageElement.setAttribute('src', '%@');", filePath];
[self.webView stringByEvaluatingJavaScriptFromString:javascript];

In your HTML the path would look like something like:

<html>
<body>
<img id="localFile" src="file:///var/mobile/Applications/3D7D43E8-FA5E-4B19-B74C-669F7D1F3093/Documents/image.png" />
</body>
</html>

本人亲自测试过,但是没有成功,也许方法不对......

测试用静态页html源码

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>关于我们</title>
<!-- When the value is true and the page contains the viewport meta tag, -->
<!-- meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"-->
</head>
<style>
body,p,h1,h2,h3,h4,h5,h6,dl,dd{ margin:0; font-size:12px; font-family:"宋体";}
ol,ul{list-style:none; padding:0; margin:0;}
a{text-decoration:none;}
img{ border:none;}
.clear{zoom:1;}
.clear:after{ content:"";display:block;clear:both;} .wrap{ width:auto;}
.head{ width:auto; height:24px; background:#ffffff} .list{ width:auto; margin:0 auto; padding-top:36px; text-align:center;}
.list h3{ font-size:90px; color:#c0c0c0; line-height:170px;}
.text{ margin:30px auto; width:95%; border-bottom:1px solid #cccccc;} .text p{ margin-bottom:30px; font-size:40px; color:#757575; text-align:left; line-height:55px;}
.text2{ text-align:left;margin:0 auto; width:95%;}
.list h2{ font-size:24px; color:#000; line-height:60px;}
.text2 p{ font-size:24px; color:#c0c0c0; line-height:60px;}
.foot{ width:auto; height:62px; background:#ffffff;}
</style>
<body>
<div class="wrap">
<div class="head">
</div>
<div class="list"> <h3>未选择的路</h3>
<img src="demo.jpg" /> <div class="text"> <p>黄色的树林里分出两条路,
可惜我不能同时去涉足,
我在那路口久久伫立,
我向着一条路极目望去,
直到它消失在丛林深处。
但我却选了另外一条路,
它荒草萋萋,十分幽寂,
显得更诱人、更美丽,
虽然在这两条小路上,
都很少留下旅人的足迹,
虽然那天清晨落叶满地,
两条路都未经脚印污染。
呵,留下一条路等改日再见!
但我知道路径延绵无尽头,
恐怕我难以再回返。
也许多少年后在某个地方,
我将轻声叹息把往事回顾,
一片树林里分出两条路,
而我选了人迹更少的一条,
从此决定了我一生的道路。
</br></p>
</div>
<div class="text2">
<h2>【联系本人】</h2>
<p>电 话:159 - 1051 - 4635</br></p>
<p>邮 箱:you_xian_1030@163.com</br></p>
<p>博 客:http://www.cnblogs.com/YouXianMing/</br></p>
</div>
<div class="foot"> </div> </div> </div>
</body>
</html>

UIWebView如何加载本地图片的更多相关文章

  1. ios网络学习------4 UIWebView的加载本地数据的三种方式

    ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...

  2. android ImageLoader加载本地图片的工具类

    import android.widget.ImageView; import com.nostra13.universalimageloader.core.ImageLoader; /** * 异步 ...

  3. angular 图片加载失败 情况处理? 如何在ionic中加载本地图片 ?

    1.angular 图片加载失败 情况处理 在directive中定义组件,在ng-src错误时,调用err-src app.directive('errSrc',function(){ return ...

  4. Flutter学习笔记(19)--加载本地图片

    如需转载,请注明出处:Flutter学习笔记(19)--加载本地图片 上一篇博客正好用到了本地的图片,记录一下用法: 首先新建一个文件夹,这个文件夹要跟目录下 然后在pubspec.yaml里面声明出 ...

  5. !!!myeclipse 上加载本地图片问题,无法加载问题

    出现无法加载本地图片的问题, 原因就是把图片放到了本地项目中的image了,但是myeclipse上没有刷新 这样以后 本地的图片比在线的要快 低级错误,诶!

  6. 用UIWebView加载本地图片和gif图

    加载gif图: NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@" ...

  7. iOS --- UIWebView的加载本地数据的三种方式

    UIWebView是IOS内置的浏览器,可以浏览网页,打开文档  html/htm  pdf   docx  txt等格式的文件.  safari浏览器就是通过UIWebView做的. 服务器将MIM ...

  8. Swift - 网页控件(UIWebView)加载本地数据,文件

    使用UIWebView加载本地数据或资源有如下三种方式: 1,使用loadHTMLString方法加载HTML内容 2,使用loadRequest方法加载本地资源(也可用于加载服务器资源) 3,先将内 ...

  9. vue如何动态加载本地图片

    大家好,我是前端队长Daotin,想要获取更多前端精彩内容,关注我(全网同名),解锁前端成长新姿势. 以下正文: 今天遇到一个在vue文件中引入本地图片的问题,于是有了这篇文章. 通常,我们的一个im ...

随机推荐

  1. WPF将TextBox的边框设为圆角的

    将TextBox的边框设为圆角的,因为TextBox默认的样式中边框就是由Border类型来实现的, 所以只需要真的当前的TextBox的Border修改属性即可,为了不影响界面中别的Border的样 ...

  2. 遇见CUBA CLI

    原文:Meet CLI for CUBA Platform 翻译:CUBA China CUBA-Platform 官网 : https://www.cuba-platform.com CUBA Ch ...

  3. 二维码之qrencode生成(带logo)

    从github下载的qrencode没有QRCodeGenerator文件,需要引入 // // QR Code Generator - generates UIImage from NSString ...

  4. [转]创建一个 Microsoft Outlook 扩展插件

    本文转自:https://coyee.com/article/10625-how-to-create-an-add-in-for-microsoft-outlook 下载示例 1.介绍 Visual ...

  5. vue权限路由实现方式总结

    使用全局路由守卫 实现 前端定义好路由,并且在路由上标记相应的权限信息 const routerMap = [ { path: '/permission', component: Layout, re ...

  6. jQuery源码分析-03构造jQuery对象-源码结构和核心函数

    3. 构造jQuery对象 3.1源码结构 先看看总体结构,再做分解: (function( window, undefined ) { var jQuery = (function() { // 构 ...

  7. mvc手把手教你写excel导入

    实习狗的每天新知识日常 准备工作: 1.在项目中添加对NPOI的引用,NPOI下载地址:http://npoi.codeplex.com/releases/view/38113 2.NPOI学习系列教 ...

  8. Java基础教程(10)--类

    一.声明类   你已经见过了以如下方式定义的类: class MyClass { // field, constructor, and method declarations }   上面是声明类的最 ...

  9. PHP · MySQL函数

    连接名=mysql_connect("主机","用户名","密码"); 连接名=mysql_qconnect("主机", ...

  10. javaScript 简单的时间格式转换【转】

    转自:http://blog.csdn.net/lxl_family/article/details/38693903.根据时间戳,转成相对的字符串形式 function timeStamp2Stri ...