从uiwebview访问相机? [英] access camera from uiwebview?

查看:19
本文介绍了从uiwebview访问相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 uiwebview 访问 iphone 的相机?我知道 phonegap 可以,但是如何在没有 phonegap 的情况下使其工作?

Is it possible to access iphone's camera from uiwebview? I know phonegap does it, but how to make it work without phonegap?

谢谢.

推荐答案

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

You can implement the UIWebView Delegate and intercept any attempted page load. By setting a custom url, you can pass a message to Objective C, that can launch the camera. To send data back to the web weiw you can initiate a new load (as I do in my example), or pass in some javascript using another method on UIWebView.

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

Here is a working example, I just wrote:

    #import "WebViewCamAppDelegate.h"
    #import <UIKit/UIKit.h>
    @interface uiwebviewcameraAppDelegate : NSObject <UIApplicationDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIWebViewDelegate> {
        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 = @"<br><A href="showcamera:">Show Camera</a>";


    //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

这篇关于从uiwebview访问相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆