如何判断cameraDevice是否可用?
在我当前的应用程序中,我允许用户向在线图像服务提交图像。我允许用户从相册中选择或使用相机拍照。
但是,我有一个问题。如果正在使用的设备没有相机并且用户选择拍照,则应用程序会崩溃。我需要能够确定设备是否能够使用cameraDevice。
下面是我当前用于呈现 UIActionSheet 的代码,它允许用户选择不同的选项。
#pragma mark -
#pragma mark UIImagePickerController
- (IBAction)ImagePicker {
UIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle:@"" delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Choose An Existing Photo", @"Take A Photo", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
[sheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
//Okay the UIImagePickerControllerSourceTypeSavedPhotosAlbum displays the
NSLog(@"Album");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
} else if (buttonIndex == 1) {
NSLog(@"Camera");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
提前致谢!
In my current application I have allowed the user to submit an image to an image service online. I allow the user to select from either their Photo Album or take a picture with the Camera.
However, I have an issue. If the device that is being used doesn't have a camera and the user selects to take a photo, the application crashes. I need to be able to determine whether or not the device has the ability to use the cameraDevice.
Below is my current code for presenting an UIActionSheet which allows the user to select the different options.
#pragma mark -
#pragma mark UIImagePickerController
- (IBAction)ImagePicker {
UIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle:@"" delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Choose An Existing Photo", @"Take A Photo", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
[sheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
//Okay the UIImagePickerControllerSourceTypeSavedPhotosAlbum displays the
NSLog(@"Album");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
} else if (buttonIndex == 1) {
NSLog(@"Camera");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西也有效:
Something like this also works:
我不会忘记,我必须添加标签和一个新属性,一个名为sheet的UIActionSheet。
下面的代码是我如何让一切完全无缝地工作的。
I won't forget, I had to add tags and a new property, a UIActionSheet called sheet.
The following code is how I got everything to work, absolutely seamlessly.
我得到了更好的解决方案;订购按钮标题和清晰的代码。
希望我的回答对您的问题有所帮助。
I got a better solution; ordering button title and clear code.
I hope my answer is helpful to your problem.