Search code examples
iphoneiosobjective-cios6uiimageview

Storyboard passing Images between views


I'm trying to pass an image from one view to another. I have a camera button and when press fires off the uiimagepickercontroller. Once the image is selected the edit photo screen is pushed onto the screen. Whenever the screen loads nothing appears. I tried following this post Passing image from one view to another but it seems not to work.

Here is my code for the first view controller:

firstViewController.h

@interface firstViewController : UITableViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>

- (IBAction)cameraButtonPressed:(id)sender;

firstViewController.m

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [self dismissViewControllerAnimated:YES completion:^{

        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        ECDEditViewController *viewController = [[ECDEditViewController alloc] initWithImage:image];

        UINavigationController *composeController = [self.storyboard instantiateViewControllerWithIdentifier:@"showCompose"];
        //[self.navigationController presentViewController:composeController animated:YES completion:nil];
        [self.navigationController pushViewController:composeController animated:YES];

    }];

}

secondViewController.h

@interface ECDEditViewController : UITableViewController <UIImagePickerControllerDelegate>

- (id)initWithImage:(UIImage *)aImage;

@property (strong, nonatomic) IBOutlet UIImageView *myImage;
@property (nonatomic, strong) UIImage *image;

secondViewController.m

@implementation ECDEditViewController
@synthesize myImage;
@synthesize image;


- (id)initWithImage:(UIImage *)aImage {

    self = [super init];
    if (self) {
        if (!aImage) {
            NSLog(@"sorry no picture loaded®");
            return nil;
        }
        self.image = aImage;
        [myImage setImage:aImage];
        NSLog(@"picture loaded");
   }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"im trying");

    [myImage setImage:image];
    [myImage setContentMode:UIViewContentModeScaleAspectFit];
}

enter image description here


Solution

  • You need to do something like this:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    
    [self dismissViewControllerAnimated:YES completion:^{ 
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; 
    
    ECDEditViewController *composeController = [self.storyboard instantiateViewControllerWithIdentifier:@"showCompose"]; 
    composeController.image = image; 
    [self.navigationController pushViewController:composeController animated:YES]; 
    
    }]; 
    
    }
    

    Your composeController is actually an ECDEditViewController, not a UINavigationController. So this line is unnecessary (as is the initWithImage: method):

     ECDEditViewController *viewController = [[ECDEditViewController alloc] initWithImage:image];