当前位置:  开发笔记 > 编程语言 > 正文

从uiwebview访问相机?

如何解决《从uiwebview访问相机?》经验,为你挑选了1个好方法。

是否可以从uiwebview访问iphone的相机?我知道phonegap会这样做,但如何让它在没有phonegap的情况下工作?

谢谢.



1> Brad The App..:

您可以实现UIWebView Delegate并拦截任何尝试的页面加载.通过设置自定义URL,您可以将消息传递给可以启动摄像头的Objective C. 要将数据发送回Web,我们可以启动新的加载(就像我在我的示例中所做的那样),或者在UIWebView上使用另一种方法传递一些javascript.

这是一个工作示例,我刚写道:

    #import "WebViewCamAppDelegate.h"
    #import 
    @interface uiwebviewcameraAppDelegate : NSObject  {
        UIWindow *window;
     UIViewController *viewController;
     UIWebView *webView; 
    }

    @property (nonatomic, retain) IBOutlet UIWindow *window;

    @end


    @implementation uiwebviewcameraAppDelegate

    @synthesize window;

    //This is the HTML we initially show in the WebView.  Note the url "showcamera:" is one I
    //invented with the intent to intercept it to show the camera.
    static NSString *htmlString = @"
Show Camera"; //Pretty Basic stuff. We set the UIWebView Delegate so we can intercept the call and set up //a ViewController so we can animate the UIImagePicker - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { viewController = [[UIViewController alloc] init]; webView = [[UIWebView alloc] initWithFrame:window.bounds]; [webView loadHTMLString:htmlString baseURL:nil]; webView.delegate = self; [viewController.view addSubview:webView]; [window addSubview:viewController.view]; [window makeKeyAndVisible]; return YES; } //Note: I check to make sure the camera is available. If it is not (iPod touch or Simulator) I show the photo library instead. -(void) showCamera { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; } else { imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } imagePicker.delegate = self; [viewController presentModalViewController:imagePicker animated:YES]; } //Here we intercept any time the webview tries to load a document. When the user hits our "showcamera: url" we go to work. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if ([[[request URL] scheme] isEqualToString:@"showcamera"]) { [self showCamera]; return NO; } return YES; } //After the imagepicker has done it's thing, we pass the data back to the webview to be displayed. //If we wanted to be fancy here, we could have done this via Javascript so we could dynamically insert an image without reloading the page. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [viewController dismissModalViewControllerAnimated:YES]; UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation (image, 0.5); [webView loadData:imageData MIMEType:@"image/jpeg" textEncodingName:@"UTF-8" baseURL:nil]; } - (void)dealloc { [viewController release]; [webView release]; [window release]; [super dealloc]; } @end

推荐阅读
LEEstarmmmmm
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有