2010-02-17 70 views
0

這是我到目前爲止的代碼:iPhone SDK - 如何在UINavigationController中顯示用相機拍攝的照片?

/* class: myViewController 
@interface myViewController: UIViewController 
     <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 
*/ 
- (IBAction) getPicture { 
    UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     [self presentModalViewController:picker animated:YES]; 
} 

- (void) imagePickerController:(UIImagePickerController *)thePicker 
        didFinishPickingMediaWithInfo:(NSDictionary *)imageInfo 
{ 
    [[thePicker parentViewController] dismissModalViewControllerAnimated:YES]; 
     UIImage *img = [imageInfo 
          objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    self.myImageView.image = img; 
} 

所以基本上我想從iPhone的相機拍照,並在一個UIImageView顯示。只要類myViewController作爲獨立視圖顯示,此功能就可以很好地工作。如果我將視圖放在UINavigationController中,UIImageView在用相機拍攝後不會顯示圖像。但是如果我從圖書館中選擇一張照片,一切都很好。

那麼,爲什麼UIImageView不會在UINavigationController中顯示用相機拍攝的圖像呢?

回答

0

嘗試以下行切換:

[thePicker dismissModalViewControllerAnimated:YES]; 
UIImage *img = [imageInfo objectForKey:@"UIImagePickerControllerOriginalImage"]; 
self.myImageView.image = img; 

以下幾點:

UIImage *img = [imageInfo objectForKey:@"UIImagePickerControllerOriginalImage"]; 
self.myImageView.image = img; 
[thePicker dismissModalViewControllerAnimated:YES]; 

也許圖像被釋放,因爲該視圖的解聘的你

self.myImageView.image = img; 
保留之前
+0

我曾經嘗試這樣做沒有任何的運氣。 – dan 2010-02-17 14:47:31

+0

設置圖像後,您是否嘗試過以下方法? [self setNeedsDisplay]; – Shingoo 2010-02-17 14:59:39

+0

不幸的是它並沒有改變任何東西。 – dan 2010-02-17 21:01:49

0

對我來說,你會忽略錯誤的視圖控制器。切忌:

[thePicker dismissModalViewControllerAnimated:YES]; 

讀:

[self dismissModalViewControllerAnimated:YES]; 
+0

我剛試過這個,但它沒有任何區別。你能再解釋一下嗎? ThePicker應該是當前的ImagePicker。 – dan 2010-02-17 15:01:45

+0

thePicker是當前的ImagePicker,但我認爲你應該將dismissModalViewControllerAnimated調用發送到創建模式視圖控制器而不是模式視圖控制器本身的視圖控制器 – pheelicks 2010-02-17 15:09:29

+0

任何一個都可以工作。 – 2010-12-26 07:34:42

相關問題