线上html

<!DOCTYPE html>
<html>
<head>
<title>HTML中用JS调用OC方法</title>
<style> #div1 {
background-color:#ccc;
width:100px;
height:100px;
position:absolute;
top:400px
} </style> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<script> function getImg(url) { var oDiv = document.getElementById("div1");
var oImg = document.getElementById("img1"); oImg.src = url; } </script>
</head>
<body> <p id="div1">
<img id="img1" src="" height="200" width="200" />
</p> <br/> <a href='ios://openMyAlbum'>打开相册</a><br><br/> </body> </html>

在oc中

//
// ViewController.m
//
// Created by Hwangkop on 15/11/30.
// Copyright © 2015年 Hp. All rights reserved.
// #import "ViewController.h"
#import <AVFoundation/AVFoundation.h> @interface ViewController () <UIWebViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{ UIWebView *_webView;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_webView]; NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"];
NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[_webView loadHTMLString:htmlCont baseURL:baseURL]; _webView.delegate = self; } -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { //通过url获取 js方法在oc中实现该方法
NSString *urlstr = request.URL.absoluteString; NSRange range = [urlstr rangeOfString:@"ios://"]; if (range.length!=0) { NSString *method = [urlstr substringFromIndex:(range.location+range.length)]; SEL selctor = NSSelectorFromString(method); [self performSelector:selctor withObject:nil];
} return YES; } //要实现的方法
-(void)openMyAlbum { UIImagePickerController *vc = [[UIImagePickerController alloc] init];
vc.delegate = self;
vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:vc animated:YES completion:nil]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存
[UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES]; [picker dismissViewControllerAnimated:YES completion:^{ // NSLog(@"%@", filePath);
// oc 调用js 并且传递图片路径参数
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"getImg('%@')", filePath]];
}];
} @end

最新文章

  1. tg2015 信息传递 (洛谷p2661)
  2. Linux.NET学习手记(8)
  3. Open DS
  4. easyui combotree下拉框多选赋值
  5. iOS 使用UIView的一种有效方法
  6. 前端之JavaScript第二天学习(4)-JavaScript-注释
  7. Ubuntu配置ssh server
  8. c++ map与 qt QMap insert 区别
  9. &lt;display&gt;标签的几个属性
  10. YII学习笔记-登录后的session的总结
  11. sublime前端编辑器入门与个人使用经验分享
  12. how to check if you have TURNIN successfully?
  13. jQuery - ajaxUpLoad.js
  14. [原]Jenkins(九)---jenkins分别发布多个项目到多个远程主机
  15. VS2012创建ATL工程及使用MFC测试COM组件
  16. 关于Java实现的进制转化(位运算)
  17. 服务网关zuul之三:zuul统一异常处理
  18. react-native 相对项目路径导入组件
  19. Windows 修改的hosts记录没有效果
  20. .Net学习心得

热门文章

  1. [PyTorch]PyTorch中反卷积的用法
  2. 【转】Python 字符串大小写转换
  3. SQL优化- in和not in
  4. 《用 Python 学微积分》笔记 3
  5. Codeforces Round #241 (Div. 2) B. Art Union 基础dp
  6. Gtk基础学习总结(一)
  7. CA证书,https讲解
  8. 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决
  9. 实例化后的list的默认值
  10. tinyxml解析xml