Iphone 从uiwebview访问摄像头?

Iphone 从uiwebview访问摄像头?,iphone,uiwebview,camera,Iphone,Uiwebview,Camera,可以从uiwebview访问iphone的摄像头吗?我知道,但如何让它在没有电话间隙的情况下工作呢 谢谢。您可以实现UIWebView委托并拦截任何尝试的页面加载。通过设置自定义url,您可以将消息传递给Objective C,以便启动相机。要将数据发送回web,您可以启动一个新加载(如我在示例中所做的),或者使用UIWebView上的另一个方法传入一些javascript 下面是一个工作示例,我刚刚写道: #import "WebViewCamAppDelegate.h" #i

可以从uiwebview访问iphone的摄像头吗?我知道,但如何让它在没有电话间隙的情况下工作呢


谢谢。

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

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

    #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
#导入“WebViewCamAppDelegate.h”
#进口
@接口uiwebviewcameraAppDelegate:NSObject{
UIWindow*窗口;
UIViewController*viewController;
UIWebView*webView;
}
@属性(非原子,保留)IBUIWindow*window;
@结束
@实现uiwebviewcameraAppDelegate
@合成窗口;
//这是我们最初在WebView中显示的HTML。请注意url“showcamera:”是一个I
//发明的目的是为了截取它来显示摄像机。
静态NSString*htmlString=@“
”; //非常基本的东西。我们设置UIWebView委托,以便拦截调用,并设置//ViewController,以便为UIImagePicker设置动画 -(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{ viewController=[[UIViewController alloc]init]; webView=[[UIWebView alloc]initWithFrame:window.bounds]; [webView加载htmlString:htmlString基URL:nil]; webView.delegate=self; [viewController.view addSubview:webView]; [窗口添加子视图:viewController.view]; [WindowMakeKeyandVisible]; 返回YES; } //注意:我检查以确保摄像头可用。如果不是(iPod touch或模拟器),我会显示照片库。 -(无效)显示照相机{ UIImagePickerController*imagePicker=[[UIImagePickerController alloc]init]; 如果([UIImagePickerController isSourceTypeAvailable:UIImagePickerController SourceTypeCamera]){ imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera; } 否则{ imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; } imagePicker.delegate=self; [viewController presentModalViewController:imagePicker动画:是]; } //在这里,我们在webview尝试加载文档时拦截任何时间。当用户点击我们的“showcamera:url”时,我们就开始工作。 -(BOOL)webView:(UIWebView*)webView应加载WithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)导航类型{ 如果([[[request URL]scheme]IsequalString:@“showcamera”]){ [自拍照]; 返回否; } 返回YES; } //在imagepicker完成它的工作之后,我们将数据传递回要显示的webview。 //如果我们想在这里发挥想象力,我们可以通过Javascript实现这一点,这样我们就可以在不重新加载页面的情况下动态插入图像。 -(void)imagePickerController:(UIImagePickerController*)picker未使用信息完成PickingMediaWithInfo:(NSDictionary*)信息{ [viewController解除ModalViewController激活:是]; UIImage*image=[info objectForKey:UIImagePickerControllerOriginalImage]; NSData*imageData=UIIMAGEJPEG表示法(图像,0.5); [webView loadData:imageData模拟类型:@“image/jpeg”文本编码名称:@“UTF-8”基本URL:nil]; } -(无效)解除锁定{ [视图控制器释放]; [网络视图发布]; [窗口释放]; [super dealoc]; } @结束
Hi Brad,你必须在app delegate中执行此操作,还是在设置webview的任何位置执行此操作?