iOS: 浏览UIWebView中的图片

1.加载UIWebView


2.UIWebView加载完成后,注入JS代码,让所有的图片都有超链接事件

funcwebViewDidFinishLoad(webView:UIWebView) {

letscript ="imgsrcs = Array(); images = document.getElementsByTagName(\"img\"); for (i = 0; i < images.length; i++) { image = images[i]; imageParentNode = image.parentNode; imageParentNode.removeChild(image); a = document.createElement(\"a\");a.setAttribute(\"href\", String(i)); a.appendChild(image); imageParentNode.appendChild(a); imgsrcs.push(image.src); } \n function photos() { imgsrcsStr = String(); for (i = 0; i < imgsrcs.length; i++) { imgsrcsStr += imgsrcs[i]; if (i+1 != imgsrcs.length) { imgsrcsStr += \">><<\"; } } return imgsrcsStr; }"

self.webView.stringByEvaluatingJavaScriptFromString(script)

}


3.点击图片,响应超链接事件

funcwebView(webView:UIWebView, shouldStartLoadWithRequest request:NSURLRequest, navigationType:UIWebViewNavigationType) ->Bool{

}

你可能感兴趣的:(iOS: 浏览UIWebView中的图片)