xcode UIImagePickerController,检查相机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13699256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:14:31  来源:igfitidea点击:

UIImagePickerController,Check camera

iphoneiosxcodeuiimagepickercontroller

提问by Paras Joshi

-(void)viewDidLoad
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
   {
      imagePicker = [[UIImagePickerController alloc] init];
      imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
      imagePicker.showsCameraControls = NO;
      [self.view addSubview:imagePicker.view];
      }
      else
      {
      // UIAlertView…
      }
}

    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:NO completion:NO];
    }

I want to put out an alert when you do not have a camera. IPhone app launch and move in this code. But, Crash (This Error >

我想在你没有相机时发出警报。iPhone 应用程序启动并在此代码中移动。但是,崩溃(此错误 >

return UIApplicationMain(argc, argv, nil, NSStringFromClass([CameraAppDelegate class])); > Thread 1: signal SIGABRT)when run in a simulator.

return UIApplicationMain(argc, argv, nil, NSStringFromClass([CameraAppDelegate class])); > Thread 1: signal SIGABRT)在模拟器中运行时。

Why is this?

为什么是这样?

回答by Paras Joshi

use this code and add UIImagePickerControllerDelegatedelegate in .hfile

使用此代码并UIImagePickerControllerDelegate.h文件中添加委托

 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  
    {
       UIImagePickerController* picker = [[UIImagePickerController alloc] init];
       picker.sourceType = UIImagePickerControllerSourceTypeCamera;
       picker.delegate = self;
      picker.wantsFullScreenLayout = YES;
      [self presentModalViewController:picker animated:YES];
    }
    else
    {
        UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:@"Camera Not Available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        altnot.tag=103;
        [altnot show];
        [altnot release];

    }

回答by Mehul Mistri

Create NSObject Class and name it like ClsGlobal, or the name you want..

创建 NSObject 类并将其命名为ClsGlobal,或者您想要的名称。

then write +(BOOL)isCameraDeviceAvailablein your ClsGlobal.hand implement below function in ClsGlobal.m.

然后写入+(BOOL)isCameraDeviceAvailable您的ClsGlobal.h并在ClsGlobal.m.

+(BOOL)isCameraDeviceAvailable
{
    BOOL isCameraAvailable=NO;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] || [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
            isCameraAvailable = YES;
    }
    return isCameraAvailable;
}

Use this class method, It will return YES if camera available else return NO.

使用这个类方法,如果相机可用则返回YES,否则返回NO。

Now you can call this method using [ClsGlobal isCameraDeviceAvailable];means your if Condition looks like if([ClsGlobal isCameraDeviceAvailable]).

现在,您可以使用[ClsGlobal isCameraDeviceAvailable];if 条件看起来像这样的方式调用此方法if([ClsGlobal isCameraDeviceAvailable])

This method will help you throughout your project in any controller, You have to just import ClsGlobal like #import "ClsGlobal.h".

此方法将在任何控制器中帮助您完成整个项目,您只需导入 ClsGlobal 之类的#import "ClsGlobal.h".