2012-07-17 62 views
1

我有一個應用程序,使用UITabBarController和其中一個標籤應該是一個完整的相機視圖(類似於Instagram)。當我啓動應用程序並轉到該視圖時,大部分屏幕都是空白的,並且沒有加載圖像庫選擇器/相機視圖。任何幫助將不勝感激。使UIViewController UIImagePicker點擊時

這裏是我的viewDidLoad方法:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    imagePicker = [[UIImagePickerController alloc] init]; 

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    } else { 
     [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    } 

    [imagePicker setDelegate:self]; 

    [imagePicker setEditing:NO]; 

    // Place image picker (camera viewing angle on the screen) 
    [self presentViewController:imagePicker animated:YES completion:nil]; 
} 

回答

0

嘗試把在viewDidAppear代碼代替 - 爲我工作。更好的辦法是,將最後一行放在viewDidLoad中,因此只調用一次,並將最後一行放在viewDidAppear方法中。要取消影像選擇器(我測試的唯一的事),它只是重新出現,因爲viewDidAppear將會再次調用,所以你需要實現imagePickerControllerDidCancel:這樣讓這樣的事情發生:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    self.imagePicker = nil; 
} 

而且,在你的viewDidAppear中,在if語句中加入一條if語句,看看picker是否存在,然後再嘗試呈現它:

if (self.imagePicker) { 
    [self presentViewController:self.imagePicker animated:YES completion:nil]; 
    } 
+0

也適合我!非常感謝!爲什麼是這樣? – user1530580 2012-07-17 05:05:09

+0

我不確定,希望別人會發布原因。 – rdelmar 2012-07-17 05:11:03