当设备在 Objective-C 中没有相机时如何禁用 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];
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用它来检查相机是否可用
Use this to check if camera is available or not