2010-10-14 67 views
3

在我目前的應用程序中,我允許用戶在線提交圖像到圖像服務。我允許用戶從他們的相冊中進行選擇或者使用相機拍照。如何確定cameraDevice是否可用?

但是,我有一個問題。如果正在使用的設備沒有攝像頭,並且用戶選擇拍攝照片,則應用程序會崩潰。我需要能夠確定設備是否有能力使用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]; 
} 
} 

在此先感謝!

回答

6
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"No camera!"); } 
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; 
} 
} 
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]; 
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]; 

我希望我的回答對您的問題有所幫助。