当设备没有物镜-C中的摄像头时,如何禁用UIActionSheet相机按钮

时间:2012-02-13 06:14:52

标签: objective-c xcode uibutton uiimagepickercontroller uiactionsheet

我有一个问题。在我的应用程序中,我有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];
}

1 个答案:

答案 0 :(得分:2)

用它来检查相机是否可用

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];