如何确定相机设备是否可用?

3
在我的当前应用程序中,我允许用户将图像提交到在线图像服务。我允许用户从照片库中选择或使用相机拍摄照片。
然而,我有一个问题。如果正在使用的设备没有相机并且用户选择拍照,则应用程序会崩溃。我需要确定设备是否能够使用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];
}
}

提前感谢您!

4个回答

6
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"No camera!"); }

1

像这样的东西也是可以工作的:

NSString* b1 = @"Get from album";
NSString* b2 = nil;

BOOL cameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

if ( cameraAvailable ) {
     b2 = @"Take a photo";   
}

UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle: alertTitle
                                                    delegate: self
                                           cancelButtonTitle: @"Cancel"
                                      destructiveButtonTitle: nil
                                           otherButtonTitles: b1, b2, nil];         
[sheet showInView: self.view];
[sheet release];

0

我不会忘记,我必须添加标签和一个新属性,一个名为sheet的UIActionSheet。 以下代码是我如何使一切都能够完美无缝地运行。

#pragma mark -
#pragma mark UIImagePickerController
- (IBAction)ImagePicker {

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sheet = [[UIActionSheet alloc] 
                        initWithTitle:@"" delegate:self 
                        cancelButtonTitle:@"Cancel" 
                        destructiveButtonTitle:nil 
                        otherButtonTitles:@"Choose An Existing Photo", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
sheet.tag = 0;
[sheet release];
}

else {
    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.tag = 1;
    [sheet release];
}

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (sheet.tag) {
    case 0:
        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];

        }
        break;
    case 1:
        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];
        }
        break;
}
}

0

我有一个更好的解决方案; 排序按钮标题和清除代码。

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil                                                              
                                                otherButtonTitles:nil];
BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if (isCameraAvailable) {
    [actionSheet addButtonWithTitle: NSLocalizedString(@"Take New Photo", @"")];
}
[actionSheet addButtonWithTitle: NSLocalizedString(@"Choose from Library", @"")];
[actionSheet addButtonWithTitle: NSLocalizedString(@"Cancel", @"")];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons -1;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

[actionSheet showInView: self.view];

我希望我的回答对你的问题有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接