2013-10-21 76 views
0

我的要求是編寫一個可自動捕捉相機圖片的示例IOS應用程序。使用各種SO環節提供我做執行下面的代碼 -在IOS中自動捕捉圖片

我CameraViewController.h類的定義如下:

@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 

@property (strong, nonatomic) IBOutlet UIImageView *ImageView; 

@end 

而且CameraViewController.m具有下面的代碼:

-(void)viewDidAppear:(BOOL)animated 
{ 
    NSLog(@"Setting the background now"); 

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self; 
    picker.allowsEditing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; 
    picker.cameraDevice = UIImagePickerControllerCameraDeviceRear; 
    picker.showsCameraControls = NO; 
    picker.navigationBarHidden = NO; 
    picker.toolbarHidden = NO; 
    [self presentViewController:picker animated:YES completion:NULL]; 

    NSLog(@"Taking the picture now"); 
    [picker takePicture]; 


} 


-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

{ 
    NSLog(@"Entered the case of finishing pictures"); 
} 

- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker 
{ 
    NSLog(@"Entered the case of cancel"); 

} 

上面的代碼所做的是成功啓動相機應用程序,但我不確定如果takePicture API能夠成功點擊圖片。我在Ipad內的照片應用程序中看不到任何保存的照片,因此我認爲該照片未被點擊。 是否有人可以告訴我,如果我上面的代碼是正確的還是什麼,我需要做的,自動點擊捕捉按鈕,進入相機控件顯示

回答

2

[請去「使用的UIImagePickerController選擇照片,拍照」蘋果文檔UIImagePickerController類的屬性cameraOverlayView爲一個完整的示例應用程序中已經做了你需要什麼,並更多]

您指定您CameraViewController如採用UIImagePickerControllerDelegate協議,因此您必須實現兩個消息:

- (void) imagePickerController: (UIImagePickerController *) picker  
    didFinishPickingMediaWithInfo: (NSDictionary *) info; 

- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker; 

正如iOS文檔所述,NSDictionary* info有一個密鑰UIImagePickerControllerOriginalImage,它將返回UIImage。訪問它,就像這樣:

UIImage *snapshot = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage]; 

因爲你的計劃是使用takePicture然後務必註明

picker.showsCameraControls = NO; 
+0

今天我做了更多的閱讀。我的要求是在顯示相機預覽後自動點擊圖片(用戶不應手動點擊相機應用程序上的按鈕)。我的假設是,takePicture API將?在我的情況下,didFinishPickingMediaWithInfo將無法正常工作,因爲用戶無法手動點擊或選擇「使用照片」選項 – sim

+0

Apple文檔要求在編程調用'takePicture'時不顯示圖像選擇器控件。看到我上面的編輯。 – GoZoner

+0

好吧我沒有將圖像選取器控件添加到否,我用最新的代碼編輯了我的原始問題,但它仍然沒有輸入didFinishPickingMediaWithInfo函數。我懷疑是不是實際上點擊圖片?我還能怎樣才能進入didFinishPickingMediaWithInfo API? – sim

1

您需要實現UIImagePIckerControllerDelegate的imagePickerController部分:didFinishPickingMediaWithInfo:方法。

之後,看看mediaInfo字典裏面有一個你可以使用的UIImage。

+0

我在日誌語句中添加了下面的代碼,但它沒有輸入這個函數,是否有我缺少的東西? - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {NSLog(@「Entered the case of finishing pictures」); } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {} – sim

0

我知道這是舊的自動拍照(W/O用戶交互),但使用定時器(請參閱接受的答案中的註釋)的更好選擇是實現完成處理程序,而不是傳入NULL。

[self presentViewController:picker animated:YES completion:^{ 
    NSLog(@"Taking the picture now"); 
    [picker takePicture]; 
}]; 

這樣一來,畫面始終採取了一切的時候,你不要浪費時間添加不必要的延誤。