2010-11-03 48 views
10

大家好我想製作一個相機應用程序。我這樣做,因爲Uiimagepicker對於顯示相機

picker.sourceType = UIImagePickerControllerSourceTypeCamera; 

其中選擇器是UIimagePicker控制器的對象。

但是,當運行代碼時,應用程序終止顯示錯誤。

終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,理由是:「源型不可用」

我用這對模擬器。我知道在模擬器中檢查相機是不可能的,但我們可以測試它。我認爲這可能是因爲相機不可用這就是爲什麼它終止。 但我看到一個具有相同代碼的應用程序,但它在模擬器上運行,只顯示相機視圖。 只是幫我解決這個問題。而且,我怎樣才能將自定義視圖添加到該應用中的相機?

回答

21

在設置源類型之前,您需要檢查設備是否有可用的攝像頭。

以下內容可以檢查設備是否有攝像頭可用。

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
} 

您無法從模擬器檢查相機功能。您可以將UIImagePickerControllerSourceTypePhotoLibrary作爲要在模擬器上測試的sourceType。

+0

感謝切塔尼亞我已經做到了,但我想知道的是該應用程序是如何展示在模擬器上的相機視圖,但不能因爲需要硬件而點擊。 – Sabby 2010-11-03 11:48:44

+0

操作系統需要驅動程序來操作外部設備。 iOS沒有MacBook相機的驅動程序。 – 2014-04-02 22:06:00

0

把下面的代碼放在發生異常的地方。請記住,你需要實現navigationController

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
      UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"ERROR" message:@"No Camera Avalible" preferredStyle:UIAlertControllerStyleAlert]; 

     UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { 
      [self dismissViewControllerAnimated:alertView completion:nil]; 
     }]; 
     [alertView addAction:ok]; 
     [self.navigationController presentViewController:alertView animated:YES completion:nil]; 
    } 
1

雨燕2.2

if UIImagePickerController.isSourceTypeAvailable(.Camera) { 
    imagePicker.delegate = self 
    imagePicker.sourceType = .Camera 
    presentViewController(imagePicker, animated: true, completion: nil) 
} else { 
    print("The device has no camera") 
} 

保存的照片專輯

if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) { 
    imagePicker.delegate = self 
    imagePicker.sourceType = .SavedPhotosAlbum 
    imagePicker.allowsEditing = false 
    self.presentViewController(imagePicker, animated: true, completion: nil) 
}