600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > h5 调用ios原生相机拍照上传照片

h5 调用ios原生相机拍照上传照片

时间:2020-11-23 15:44:38

相关推荐

h5 调用ios原生相机拍照上传照片

1.html中的点击按钮和回调显示标签---------------直接上代码

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body><input type="button" name="" value="拍照" onclick="takeIDcardImg()"> <!--点击拍照调用相机--><br/><p id="result">扫描结果:</p> <!--原生给h5值---插入html上--><script>//调用APP的扫描方法 h5->appfunction takeIDcardImg(){app.takeIDcardImg('imgBase');}//扫描结果回调方法 app->h5function imgBase(result){document.getElementById("result").innerHTML = '扫描结果:' + result;}</script></body></html>

2.使用---------- JavaScriptCore的----这里是创建了一个类,也可以用块回调。

(1)。创建AppJSObject类继承NSObject .----------- AppJSObject.h

#import <Foundation/Foundation.h>#import <JavaScriptCore/JavaScriptCore.h>NS_ASSUME_NONNULL_BEGIN@protocol AppJSObjectDelegate <JSExport>-(void)takeIDcardImg:(NSString *)message;//--takeIDcardImg--方法名要是html中保持一致@end@interface AppJSObject : NSObject<AppJSObjectDelegate>@property(nonatomic,weak) id<AppJSObjectDelegate> delegate;@endNS_ASSUME_NONNULL_END

(2).AppJSObject.m

-(void)takeIDcardImg:(NSString *)message{NSLog(@"%@",message);[self.delegate takeIDcardImg:message];}

3.ViewController控制起中

#import "WebViewController.h"#import <JavaScriptCore/JavaScriptCore.h>#import "AppJSObject.h"@interface WebViewController ()<UIWebViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, AppJSObjectDelegate>@property (nonatomic, strong) UIWebView * webView;@property (nonatomic, strong) UIImageView * imageVC;@end@implementation WebViewController- (void)viewDidLoad {[super viewDidLoad];self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];self.webView.dataDetectorTypes = UIDataDetectorTypeAll;[self.view addSubview:_webView];self.webView.delegate = self;[self loadExamplePage:_webView];//本地加载}- (void)loadExamplePage:(UIWebView*)webView {NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];[webView loadHTMLString:appHtml baseURL:baseURL];}-(void)webViewDidFinishLoad:(UIWebView *)webView{//NSLog(@"------UserAgent = %@", [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]);JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];AppJSObject *jsObject = [AppJSObject new];jsObject.delegate = self;context[@"app"] = jsObject;}-(void)takeIDcardImg:(NSString *)message {[self openCamera];}/*** 调用照相机*/- (void)openCamera {UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self;picker.allowsEditing = YES; //可编辑//判断是否可以打开照相机if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){//摄像头picker.sourceType = UIImagePickerControllerSourceTypeCamera;//出现这个问题,基本就是UI操作放在了非主线程中操作导致。我的问题是webview的回调,有时候会进入子线程处理。所以统一加上dispatch_async(dispatch_get_main_queue...dispatch_async(dispatch_get_main_queue(), ^{ //不加这句有时候点击会闪退 [self presentViewController:picker animated:YES completion:nil];});}else{NSLog(@"没有摄像头");}}#pragma mark - UIImagePickerControllerDelegate// 拍照完成回调//参数:图片选择器 字典参数-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{//通过key值获取到图片UIImage * image =info[UIImagePickerControllerOriginalImage];//NSLog(@"image=%@ info=%@",image, info);//判断数据源类型if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {//设置图片背景// NSUserDefaults * user = [NSUserDefaults standardUserDefaults];// NSData* imgData = UIImageJPEGRepresentation(image, 1);self.imageVC.image = image;[self dismissViewControllerAnimated:YES completion:nil];}if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {NSLog(@"在相机中选择图片");UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//设置图片背景self.imageVC.image = image;// NSData *data = UIImageJPEGRepresentation(image, 1.0f);//// NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];//新的方法NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);NSString *dataStr = [imageData base64Encoding];//这个base64方法被遗弃了,但是前端能接受到,最新方法,他接受不到只能将就了JSContext *context=[self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];NSString *alertJS= [NSString stringWithFormat:@"imgBase('%@')",dataStr];//准备执行的js代码 --这里的imgBase要与html 中保持一致[context evaluateScript:alertJS];//通过oc方法调用jsdispatch_async(dispatch_get_main_queue(), ^{[self dismissViewControllerAnimated:YES completion:nil];});}}//进入拍摄页面点击取消按钮- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{dispatch_async(dispatch_get_main_queue(), ^{[self dismissViewControllerAnimated:YES completion:nil];});}

4.还有就是前端要判断手机类型.UserAgent需要修改下在appdelegate中添加

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];NSString *newUserAgent = [userAgent stringByAppendingString:@"IOSAPP"];//自定义需要拼接的字符串NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];}

5.还有几种方法。参考链接。/dolacmeng/article/details/79623708

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。