iOS UIWebView 中 js调用OC 打开相册 获取图片, OC调用js 将图片加载到html上
2024-10-19 13:23:27
线上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
最新文章
- tg2015 信息传递 (洛谷p2661)
- Linux.NET学习手记(8)
- Open DS
- easyui combotree下拉框多选赋值
- iOS 使用UIView的一种有效方法
- 前端之JavaScript第二天学习(4)-JavaScript-注释
- Ubuntu配置ssh server
- c++ map与 qt QMap insert 区别
- <;display>;标签的几个属性
- YII学习笔记-登录后的session的总结
- sublime前端编辑器入门与个人使用经验分享
- how to check if you have TURNIN successfully?
- jQuery - ajaxUpLoad.js
- [原]Jenkins(九)---jenkins分别发布多个项目到多个远程主机
- VS2012创建ATL工程及使用MFC测试COM组件
- 关于Java实现的进制转化(位运算)
- 服务网关zuul之三:zuul统一异常处理
- react-native 相对项目路径导入组件
- Windows 修改的hosts记录没有效果
- .Net学习心得
热门文章
- [PyTorch]PyTorch中反卷积的用法
- 【转】Python 字符串大小写转换
- SQL优化- in和not in
- 《用 Python 学微积分》笔记 3
- Codeforces Round #241 (Div. 2) B. Art Union 基础dp
- Gtk基础学习总结(一)
- CA证书,https讲解
- 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决
- 实例化后的list的默认值
- tinyxml解析xml