Search code examples
iphoneiosuiimageviewmaster-detail

UIImage not loading in the first instance in DetailViewController


I have a master-detail application. I list all images from the application documents folder in the MasterViewController tableview. On the click of each tableview item, I load the image in the DetailViewController, which has a UIImageView. The problem I am facing is that, the images does not load in the first instance, when I click a tableview item. I just see the message "The detail view content goes here", in the DetailViewController. If I go back and try again, it will work just fine. Its just the first time that the image wont load.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 if (!self.detailViewController) {
    self.detailViewController = [[PtmDetailViewController alloc] initWithNibName:@"PtmDetailViewController" bundle:nil];
}
//NSDate *object = _objects[indexPath.row];

NSData *imgData = [NSData dataWithContentsOfFile:self.fPath];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
self.detailViewController.imgViewer.image = thumbNail;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}

What am I doing wrong? Any help would be appreciated.


Solution

  • It seems like the view of detailViewController doesn't have time to load. So you are setting the image to the nil UIImageView (it's not initialized yet).

    Try adding a property UIImage *thumb to detailViewController and then in detailViewController's viewDidLoad add

    imgViewer.image=self.thumb;
    

    And in your tableView:didSelectRowAtIndexPath: change

    UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
    self.detailViewController.imgViewer.image = thumbNail;
    

    to

    UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
    self.detailViewController.thumb = thumbNail;