如何在目标C中没有相机时禁用UIActionSheet相机按钮



我有个问题。在我的应用程序中,我有两个按钮的UIActionSheet,一个是拍照,另一个是从库中选择照片。我的问题是如何检查设备是否有摄像头可用,如果没有,我如何从操作表中禁用拍照按钮。我尝试了很多东西,但都不起作用。我在下面添加代码。请让我知道我的代码出了什么问题。如果我将拍照按钮设置为禁用,那么我会收到一个错误,说"发送了无法识别的选择器"。

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

使用此项检查相机是否可用

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

最新更新