在我目前的应用程序中,我允许用户在线提交图像到图像服务。我允许用户从相册中选择或使用相机拍照。
但是,我有一个问题。如果正在使用的设备没有摄像头且用户选择拍照,则应用程序崩溃。我需要能够确定设备是否能够使用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];
}
}
提前致谢!
答案 0 :(得分:6)
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"No camera!"); }
答案 1 :(得分:1)
这样的事情也有效:
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];
答案 2 :(得分:0)
我不会忘记,我必须添加标签和一个新属性,一个名为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;
}
}
答案 3 :(得分:0)
我得到了更好的解决方案;订购按钮标题和清除代码。
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];
我希望我的回答对你的问题有帮助。