2011-06-17 50 views
13

我需要一些幫助。我需要將相機集成到我的應用程序中,並且我想了解以下內容:iPhone Xcode相機集成教程

  1. 我需要我的視圖上的相機按鈕,以便點擊它可以打開相機視圖。
  2. 我拍照
  3. 我需要編碼才能進入手機庫,然後 在另一個視圖中顯示圖片。

請問誰能指點我正確的方向?

+0

嗨訪問此鏈接http://stackoverflow.com/questions/10176020/how-to-take-picture-from-camera-using-iphone-app –

回答

52

好,UIImagePickerController是你需要的工具。它將完成該清單中的大部分內容。

對於按鈕,您可以使用圖形創建自定義按鈕,或者如果您打算使用工具欄或導航欄來按住按鈕,則可以使用UIBarButtonSystemItemCamera系統項創建欄按鈕。這會給你框架圖像。

在點擊它時,您將創建一個UIImagePickerController實例並以模態方式呈現它。

UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init]; 
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
imagePicker.delegate = self; 
[self presentModalViewController:imagePicker animated:YES]; 
[picker release]; 

正如你一定是注意到它被定義爲id < UIImagePickerControllerDelegate, UINavigationControllerDelegate> delegate;所以你將不得不採取兩種協議,但在大多數情況下,你只實現兩種方法delegate財產 - imagePickerControllerDidCancel:imagePickerController:didFinishPickingMediaWithInfo:UIImagePickerControllerDelegate協議中有另一種方法,但不推薦使用。即使你看到它在這裏提到很多,也不要使用它。你可能會認爲取消處理程序被寫成這樣,

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    [self dismissModalViewControllerAnimated:YES]; 
} 

其他方法是你做的大部分的東西。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage]; 

    // You have the image. You can use this to present the image in the next view like you require in `#3`. 

    [self dismissModalViewControllerAnimated:YES]; 
} 

拍照是由UIImagePickerController實例自動完成的。但是,如果您想要覆蓋其控件,可以將showsCameraControls設置爲NO,然後執行您自己的cameraOverlayView。如果你已經這樣做並且已經分配了一個按鈕來拍攝照片,你可以使用takePicture方法實際觸發照片動作。所以這應該解決#2

您也可以使用其他屬性來調整圖像選擇器。例如,您可以限制用戶僅使用mediaTypes屬性拍攝圖像。

+0

感謝一個很好的DUDE! – Legolas

+0

Dude !! http://stackoverflow.com/questions/6444107/help-with-modal-view-controller-login – Legolas

+1

+1一個很好的答案。很好解釋,非常感謝。 – Peter

0

解釋文檔,dismissModalViewControllerAnimated:已從iOS6開始棄用。改爲使用dismissViewControllerAnimated:completion: