如何判断cameraDevice是否可用?

发布于 2024-09-27 02:53:30 字数 1498 浏览 3 评论 0原文

在我当前的应用程序中,我允许用户向在线图像服务提交图像。我允许用户从相册中选择或使用相机拍照。

但是,我有一个问题。如果正在使用的设备没有相机并且用户选择拍照,则应用程序会崩溃。我需要能够确定设备是否能够使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还不是爱你 2024-10-04 02:53:30
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"No camera!"); }
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"No camera!"); }
¢蛋碎的人ぎ生 2024-10-04 02:53:30

像这样的东西也有效:

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];

Something like this also works:

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];
埋情葬爱 2024-10-04 02:53:30

我不会忘记,我必须添加标签和一个新属性,一个名为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;
}
}

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.

#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;
}
}
赠佳期 2024-10-04 02:53:30

我得到了更好的解决方案;订购按钮标题和清晰的代码。

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];

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

I got a better solution; ordering button title and clear code.

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];

I hope my answer is helpful to your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文