Search code examples
iosobjective-cuiviewcontrolleruiimageviewuiimage

iOS / ObjectiveC: I try to open a viewcontroller with an imageview containing a local image, but it displays a white page


I precise that when I use the debugger, I can see the image in the image view. Why it displays a white page ? (I can see the back button)

- (void) openIMAGE:(NSIndexPath *) indexPath
{
    NSString *filePath = [menuPath stringByAppendingPathComponent:contentsList[indexPath.row]];
    


    UIViewController* imageViewController = [UIViewController new];
    imageViewController.view.backgroundColor = [UIColor whiteColor];
    //0,21,73,30
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(backBtn:)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Back" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.frame = CGRectMake(0.0, 21.0, 73.0, 30.0);

    UIImageView *imageView= [UIImageView new];
    imageView.image = [UIImage imageWithContentsOfFile: filePath];
    
//    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 48.0, self.view.frame.size.width, self.view.frame.size.height - 48.0)];


    [imageViewController.view addSubview:imageView];
    [imageViewController.view addSubview:button];
    
    
    [self.navigationController pushViewController:imageViewController animated:YES];
    }

Solution

  • UIImageView *imageView= [UIImageView new];
    imageView.image = [UIImage imageWithContentsOfFile: filePath];
    [imageViewController.view addSubview:imageView];
    

    Unlike your button, your imageView has no frame and no size. Thus it is an infinitesimal point at the upper left corner and you can't see it.

    Quite apart from that, your code is atrocious. Never populate a view controller's view from another view controller. Make an image view controller class and have it populate itself.