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
UIImagePickerController,Check camera
提问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 UIImagePickerControllerDelegate
delegate in .h
file
使用此代码并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)isCameraDeviceAvailable
in your ClsGlobal.h
and 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"
.