2011-05-30 118 views
0

我正在編寫一個應用程序,該應用程序具有包含2個按鈕的初始視圖 - 一個允許用戶使用相機拍攝照片,另一個允許他選擇來自圖書館的圖片。從相機/圖庫中選擇一張照片進入不同的視圖

我已經編寫了允許這種情況發生的代碼,但是在選擇圖片後,我想轉到另一個允許分享圖片或其他內容的視圖。任何人都可以告訴我如何做像「whenPhotoIsSelected,view = newView」?

這是我到目前爲止的代碼:

#pragma mark - 
-(IBAction) getCameraPicture: (id) sender{ 
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
picker.delegate = self; 
picker.allowsImageEditing = YES; 
picker.sourceType = (sender == takePictureButton) ? 
UIImagePickerControllerSourceTypeCamera : 
UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
[self presentModalViewController:picker animated:YES]; 

[picker release]; 

}

我知道

-(void) imagePickerController:(UIImagePickerController *)picker 
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 

的存在,但我究竟是如何使用它?我試過了,它崩潰了......

+0

imagePickerController:didFinishPickingImage:editingInfo:可能是這裏的方法。它爲什麼會崩潰?你能提供源代碼嗎? – Sascha 2011-05-30 22:56:48

+0

我修改它爲imagePickerController:didFinishPickingImage:editingInfo但我在哪裏調用它?這本字典應該包含什麼?源代碼在我的文章中;我真的不知道該在哪裏調用該方法......該代碼是ViewController類實現的一部分。 – 2011-05-30 23:00:05

回答

1

在您的ViewController中實現UIImagePickerControllerDelegate。 要做到這一點添加<UIImagePickerControllerDelegate>到你的ViewController的接口聲明(在.h文件),像這樣:在您的視圖控制器的.m文件

@interface YourViewController : UIViewController <UIImagePickerControllerDelegate> { 
// instance variable declarations etc. 
} 

,那就可以實現該方法-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo而不是調用它,就像這樣:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { 
    // add the new view, for example you could push it on your navigation stack if you use a UINavigationController 
} 

當選擇圖像時,該方法將由UIImagePicker調用。

+0

它工作:)謝謝!我只是推了視圖,然後一旦我選擇了一張圖片就不再掛了。 – 2011-05-30 23:17:20

+0

現在發生的事情是,當我從圖書館中選擇圖片時,它會進入下一個視圖,但是在頂部,我也有「回到相冊」,這不應該在那裏。你能告訴我如何擺脫它嗎? – 2011-05-30 23:39:55

+0

那麼如果你使用UINavigationController,你可以在推送新的UIViewController之前發送popViewControllerAnimated:NO,或者你可以通過向UINavigationController發送setNavigationBarHidden:animated隱藏導航條,或者你只需​​要將當前視圖設置爲新視圖self.view = newView)。 – Sascha 2011-05-31 00:00:59

相關問題