#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface ViewController ()<UIWebViewDelegate>
{
UIWebView * _webView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
_webView.delegate = self;
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://news.xinhuanet.com/politics/2017-05/18/c_1120991501.htm"]]];
[self.view addSubview:_webView];
}
//网页加载完成:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self test:webView];
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"passValue"] = ^{
NSArray *arg = [JSContext currentArguments];
NSString *st = [NSString stringWithFormat:@"%@",arg[0]];
NSArray* are = [st componentsSeparatedByString:@"****"];
NSLog(@"%@",are);
};
}
- (void)test:(UIWebView*)webView{
//如果网页的正文不是content 的话, 替换成对应的标签
static NSString * const jsIN =@"function showimg() {\
var imgs = document.getElementById(\"content\").getElementsByTagName(\"img\");\
var imgScr = '';\
for (var i = 0; i < imgs.length; i++) {\
var temp = (imgScr.length > 0) ?'****':'';\
imgScr = imgScr + temp + imgs[i].src;\
};\
for (var i = 0; i < imgs.length; i++) {\
imgs[i].addEventListener(\"click\", function() {\
passValue(imgScr);\
});\
};\
}";
[webView stringByEvaluatingJavaScriptFromString:jsIN]; //注入js代码
[webView stringByEvaluatingJavaScriptFromString:@"showimg()"];//启动函数
}
网页不分静态动态(这一点比pc端好多了)
如果图片是Data URL(data:image/jpg;base64,)这种格式加载的,使用返回数据需要判断处理
转换成image代码如下
NSData *_decodedImageData = [[NSData alloc]initWithBase64EncodedString:@"your image data encoded by base64 " options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));
2.swift 版, 另外一种实现方法
需要的自己翻译成OC
代码来自于qq群
import UIKit
import Kingfisher
class WebViewController: UIViewController,UIWebViewDelegate {
@IBOutlet var kuangView: UIView!
@IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var webView: UIWebView!
var currentIndex = 0
var totalImg = 0
@IBAction func onclickImg(_ sender: Any) {
kuangView.isHidden = true
}
@IBAction func onBackImg(_ sender: Any) {
if currentIndex > 0{
currentIndex = currentIndex - 1
let url = URL(string: urlArray[currentIndex])
imageview.kf.setImage(with: url)
}
}
@IBAction func onNextImg(_ sender: Any) {
if currentIndex < totalImg - 1{
currentIndex = currentIndex + 1
let url = URL(string: urlArray[currentIndex])
imageview.kf.setImage(with: url)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var urlArray = [String]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let request = URLRequest(url: URL(string: "http://www.eshore.cn/eshoreWifi.html?userip=172.16.0.11")!)
webView.loadRequest(request)
webView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidFinishLoad(_ webView: UIWebView) {
var jsGetImages = "function getImages(){var objs = document.getElementsByTagName(\"img\");var imgScr = '';for(var i=0;i<objs.length;i++){var iii=objs[i].src;imgScr = imgScr + iii + '+';objs[i].onclick = function(){document.location.href = \"ios://jsAttachment?\"+this.src;};};return imgScr;}"
webView.stringByEvaluatingJavaScript(from: jsGetImages)
var urlResurlt = webView.stringByEvaluatingJavaScript(from: "getImages()")
urlArray = (urlResurlt?.components(separatedBy: "+"))!
totalImg = urlArray.count - 1
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let url = request.url
// print(url)
if (url!.scheme == "ios") {
let fun = url!.host
let arg0Str = url?.query?.removingPercentEncoding
// print(arg0Str)
let arg0 = arg0Str?.data(using: String.Encoding.utf8)!
switch fun!{
case "jsAttachment"://选择图片
jsAttachment(url: arg0Str!)
default:
break
}
return false;
}
return true
}
func jsAttachment(url : String){
print(url)
for (index ,value) in urlArray.enumerated(){
if value == url{
currentIndex = index
}
}
let url = URL(string: url)
imageview.kf.setImage(with: url)
kuangView.isHidden = false
}
}
网友评论