2016-03-15 85 views
0

我想在UIImagePickerController中選擇一個圖像後創建一個預覽圖像的視圖。我試圖創建一個視圖控制器,並推動選擇器控制器到另一個視圖控制器。如何在UIImagePickerController中選擇圖像後設置預覽圖像的視圖?

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    ImagePreviewController *ipc = [self.storyboard instantiateViewControllerWithIdentifier:@"ImagePreviewController"]; 
    UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage]; 

    ipc.selectedImage = image; 
    [picker pushViewController:ipc animated:YES]; 
} 

正如你所看到的,我試圖將UIImagePickerController推送到另一個視圖並嘗試預覽它。但不幸的是,當我調試它時,ipc對象顯示爲空。我明白,UIImagePickerController是從UINavigationController繼承的。是否因爲在此導航控制器中找不到我的ipc視圖?如果是的話,我如何將它添加到UIImagePickerController中,以便它可以推送到預覽圖像視圖?如果不是,我如何正確設置預覽圖像視圖?

更新:如果我把

ImagePreviewController *ipc = [ImagePreviewController alloc]init]; 

相反,當它推動,這是一個黑色的屏幕。

回答

1

您應該駁回影像選擇器,然後出示您的自定義UIViewController

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIViewController *presentingViewController = picker.presentingViewController; 
    ImagePreviewController *ipc = [self.storyboard instantiateViewControllerWithIdentifier:@"ImagePreviewController"]; 
    UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage]; 

    ipc.selectedImage = image; 

    [presentingViewController dismissViewControllerAnimated:YES completion:^(BOOL animated){ 
     [presentingViewController presentViewController:ipc animated:YES completion:nil]; 
    }]; 
} 
+0

工控機顯示這是零,但我101%肯定它在那裏。 –

+0

而且我也試過你的代碼,但不幸的是屏幕仍然是黑色的。 –

+0

原來是我的ImagePreviewController類的一些錯誤。你的代碼很好。謝謝您的幫助。 –

相關問題