当设备在 Objective-C 中没有相机时如何禁用 UIActionSheet 相机按钮

发布于 2025-01-05 11:22:49 字数 2917 浏览 0 评论 0原文

我有一个问题。在我的应用程序中,我有 UIActionSheet,有 2 个按钮,一个是拍照并从库中选择照片。我的问题是如何检查设备是否有可用的相机,如果没有,我如何禁用操作表中的拍照按钮。我尝试了很多方法,但没有任何效果。我在下面添加代码。请让我知道我的代码有什么问题。如果我将拍照按钮设置为禁用,则会收到错误消息“发送了无法识别的选择器”。

-(void)gestureRecognizer {
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapOnImage)];
[recognizer setNumberOfTapsRequired:2];
recognizer.cancelsTouchesInView = NO;
[self.employeeImage addGestureRecognizer:recognizer]; 
self.employeeImage.userInteractionEnabled = YES;
}

-(void)TapOnImage {
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"" destructiveButtonTitle:nil otherButtonTitles:@"Take photo",@"Choose photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;

//    NSString *model = [[UIDevice currentDevice]model];
//    NSLog(@"current device:%@",model);

//    if([model isEqualToString:@"iPad1,1"]) {
//    NSString *button = (NSString *)[actionSheet buttonTitleAtIndex:0];
// 
//            UIButton *btn = (UIButton *)button;
//            if ([[btn currentTitle] isEqualToString:@"Take photo"]) {
//                // Do things with button.
//                button.hidden = YES;
//            }

//    }


[actionSheet showFromRect:self.employeeImage.frame inView:self.view animated:YES];

}

- (void)displayImagePicker:(UIImagePickerController *)imagePicker {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // present from popover
    self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];

    [self.popover presentPopoverFromRect:self.employeeImage.frame//popOverRect 
                                  inView:self.view
                permittedArrowDirections:UIPopoverArrowDirectionLeft
                                animated:YES];
 } else  {
    [self presentModalViewController:imagePicker animated:YES];
}
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:  (NSInteger)buttonIndex {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;

if(buttonIndex == 0) {
    if ([UIImagePickerController  isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
else {
       UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"This device doesn't support camera" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
       [alert show];

    }
    return;
}
else if(buttonIndex == 1) {
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else {
        return;
    }
}
else {
    return;

}

[self displayImagePicker:imagePicker];
}

I have a question.In my application I have UIActionSheet with 2 buttons one is take photo and choose photo from library. My question is how to check whether the device has camera available,If not how can I disable the take photo button from the action sheet.I tried so many things but nothing worked for me. I am adding code below. Please let me know what's wrong with my code. If I set the take photo button to disable then I am getting an error saying "unrecognized selector sent".

-(void)gestureRecognizer {
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapOnImage)];
[recognizer setNumberOfTapsRequired:2];
recognizer.cancelsTouchesInView = NO;
[self.employeeImage addGestureRecognizer:recognizer]; 
self.employeeImage.userInteractionEnabled = YES;
}

-(void)TapOnImage {
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"" destructiveButtonTitle:nil otherButtonTitles:@"Take photo",@"Choose photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;

//    NSString *model = [[UIDevice currentDevice]model];
//    NSLog(@"current device:%@",model);

//    if([model isEqualToString:@"iPad1,1"]) {
//    NSString *button = (NSString *)[actionSheet buttonTitleAtIndex:0];
// 
//            UIButton *btn = (UIButton *)button;
//            if ([[btn currentTitle] isEqualToString:@"Take photo"]) {
//                // Do things with button.
//                button.hidden = YES;
//            }

//    }


[actionSheet showFromRect:self.employeeImage.frame inView:self.view animated:YES];

}

- (void)displayImagePicker:(UIImagePickerController *)imagePicker {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // present from popover
    self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];

    [self.popover presentPopoverFromRect:self.employeeImage.frame//popOverRect 
                                  inView:self.view
                permittedArrowDirections:UIPopoverArrowDirectionLeft
                                animated:YES];
 } else  {
    [self presentModalViewController:imagePicker animated:YES];
}
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:  (NSInteger)buttonIndex {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;

if(buttonIndex == 0) {
    if ([UIImagePickerController  isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
else {
       UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"This device doesn't support camera" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
       [alert show];

    }
    return;
}
else if(buttonIndex == 1) {
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else {
        return;
    }
}
else {
    return;

}

[self displayImagePicker:imagePicker];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浅紫色的梦幻 2025-01-12 11:22:49

用它来检查相机是否可用

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

Use this to check if camera is available or not

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