- 授权协议: MIT
- 开发语言: Objective-C
- 操作系统: iOS
- 软件首页: https://github.com/smallmuou/PPHTMLImagePreviewDemo
- 软件文档: https://github.com/smallmuou/PPHTMLImagePreviewDemo
软件介绍
该DEMO主要用于演示iOS APP中点击HTML的图片来预览图片,效果可以查看下面的演示图
原理阐述
1. 背景知识
在了解该原理之前,需要知道如下内容
html中的点击动作一般都是通过javascript来实现的,如下面代码:
var img = document.getElementById('test'); img.onclick = function() { alert("test"); }PS: 以上代码实现的: 点击< img>来弹出含test的提示框.
javascript与objc交互
在iOS APP开发过程中,是通过UIWebView来加载html页面,因此javascript要与objc交互,桥梁应该就在UIWebview提供的API中,可以发现如下一些接口:
//objc 传参给javascript - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script; //javascript 传参给objc, 参数是存在于request中 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
2. 实现细节
在UIWebview加载完html后,调用stringByEvaluatingJavaScriptFromString来执行如下javascript代码,可以使用javascript压缩工具压缩下
function assignImageClickAction() { var imgs = document.getElementsByTagName('img'); var length = imgs.length; for (var i = 0; i < length; i++) { img = imgs[i]; img.onclick = function() { window.location.href = 'image-preview:' + this.src } } } assignImageClickAction();在webView:shouldStartLoadWithRequest:navigationType处理image-preview
许可
该代码遵循MIT许可.
联系方式
email: lvyexuwenfa100@126.com
The Joy of X
Niall Mansfield / UIT Cambridge Ltd. / 2010-7-1 / USD 14.95
Aimed at those new to the system seeking an overall understanding first, and written in a clear, uncomplicated style, this reprint of the much-cited 1993 classic describes the standard windowing syste......一起来看看 《The Joy of X》 这本书的介绍吧!
HTML 压缩/解压工具
在线压缩/解压 HTML 代码
Markdown 在线编辑器
Markdown 在线编辑器
