ios的webView加载本地html图片路径显示及webview适配
来源:互联网 发布:arch linux chroot 编辑:程序博客网 时间:2024/06/05 04:26
在ios中,如果需要用到UIWebView,加载本地html,在本地html中,需要显示图片,路径显示的问题。
loadHTMLString时,将baseURL改为图片路径,在html文件中img标签直接改为图片名字即可。
</span><img src="image@3x.png" alt="图片" /><span class="s4"><br><br></span>
NSString *resPath = [[NSBundle mainBundle] resourcePath]; NSString *htmlFilePath = [resPath stringByAppendingPathComponent:@"index.html"]; NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"image@3x" ofType:@"png"]; if ([[NSFileManager defaultManager] fileExistsAtPath:htmlFilePath]) { NSString *string = [NSString stringWithContentsOfFile:htmlFilePath encoding:NSUTF8StringEncoding error:nil]; if (string) { NSString *htmlString = string; [_webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:imagePath]]; }}
若图片显示过大,可加入以下代码适配界面:
#pragma mark - 修改html代码字符串,适应webview+ (NSString *)adaptWebViewForHtml:(NSString *) htmlStr{ NSMutableString *headHtml = [[NSMutableString alloc] initWithCapacity:0]; [headHtml appendString : @"<html>" ]; [headHtml appendString : @"<head>" ]; [headHtml appendString : @"<meta charset=\"utf-8\">" ]; [headHtml appendString : @"<meta id=\"viewport\" name=\"viewport\" content=\"width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=false\" />" ]; [headHtml appendString : @"<meta name=\"apple-mobile-web-app-capable\" content=\"yes\" />" ]; [headHtml appendString : @"<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\" />" ]; [headHtml appendString : @"<meta name=\"black\" name=\"apple-mobile-web-app-status-bar-style\" />" ]; [headHtml appendString : @"<style>img{width:100%;}</style>" ]; [headHtml appendString : @"<style>table{width:100%;}</style>" ]; [headHtml appendString : @"<title>webview</title>" ]; NSString *bodyHtml; bodyHtml = [NSString stringWithString:headHtml]; bodyHtml = [bodyHtml stringByAppendingString:htmlStr]; return bodyHtml;}
0 0
- ios的webView加载本地html图片路径显示及webview适配
- iOS webView加载本地html,及图片等问题
- iOS WebView加载本地HTML的方法
- iOS使用webView加载本地的html文件,无法显示js和css
- WebView加载本地HTML
- webview 加载本地html
- iOS中webview加载本地html文件
- iOS webView 远程html加载本地资源
- iOS WebView加载本地html文件
- 【WebView】加载本地html及远程URL
- IOS的WebView请求远程html并加载本地资源
- webView 加载路径 里面的图片不显示
- webview加载的html文件,图片如何放大显示?
- webview加载的html文件,图片如何放大显示?
- webview加载html 及显示效果
- Webview加载本地图片的方案对比
- WebView 加载本地HTML CSS
- WebView加载本地Html文件
- Android调用webservice接收json
- 在xcode6.1和ios10.10.1环境下实现app发布
- 手机号,邮箱,车牌号合法性验证(手机号如果要支持更多的运营商的号码,比如177,要改一下正则表达式)
- 涉足计算机视觉领域要知道的
- leetcode--Add Two Numbers
- ios的webView加载本地html图片路径显示及webview适配
- surfaceview播放视频截图黑屏
- 互联网行业2015校园招聘
- 每个程序员需掌握的20个代码命名小贴士
- 程序员笑话
- NYOJ 2 括号配对问题(数据结构)
- 基于nginx搭建phpmyadmin环境
- 用Maven插件生成Mybatis代码
- LeetCode OJ Convert Sorted List to Binary Search Tree