5
votes

I'm getting an image from the user from the photo gallery, and I want to pass it to a another view...

I've read that I should be able to simply do something like this:

secView.imgView.image = selectedImage.image;

Setting my UIImageView on the second view to be the image from my UIImageView... But the UIImageView in the second view is just empty...

My code is this...

ViewController.h:

@interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    UIImagePickerController *picker;
    IBOutlet UIImageView *selectedImage;

    SecondView *secondView;
}

@property (nonatomic, retain) UIImageView *selectedImage;
@property (nonatomic, retain) SecondView *secondView;

-(IBAction)sendImage:(id)sender;
-(IBAction)openGallery:(id)sender;

@end

ViewController.m (only the relevant parts)

-(IBAction)sendImage:(id)sender
{
    if(self.secondView == nil)
    {
        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];

        self.secondView = secView;

        secView.imgView.image = selectedImage.image;
    }

    [self.navigationController pushViewController:secondView animated:YES];
}

-(IBAction)openGallery:(id)sender
{
    picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentModalViewController:picker animated:YES];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker 
{    
    [Picker dismissModalViewControllerAnimated:YES];    
}

- (void)imagePickerController:(UIImagePickerController *) Picker

didFinishPickingMediaWithInfo:(NSDictionary *)info 
{    
    // Save the image
    selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [Picker dismissModalViewControllerAnimated:YES];
}

In another post I read that another approach was to do this:

-(IBAction)sendImage:(id)sender
{
    if(self.secondView == nil)
    {
        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];

        self.secondView = secView;

        [secView setImage:selectedImage.image];
    }

    [self.navigationController pushViewController:secondView animated:YES];
}

and then in the secondview.h

-(void)setImage:(UIImage *)image;

and .m

-(void)setImage:(UIImage *)image
{
    [imgView setImage:image];
}

But both approaches results in an empty UIImageView... So what is the correct approach to doing this?

Thanks in advance!

EDIT

SecondView is fairly simple at the moment... (in the posted code I try and follow method two)

@interface SecondView : UIViewController
{
    UIImageView *imgView;
}

@property (nonatomic, retain) IBOutlet UIImageView *imgView;

-(void)setImage:(UIImage *)image;

@end

@implementation SecondView

@synthesize imgView;

-(void)setImage:(UIImage *)image
{
    [imgView setImage:image];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

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

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

SOLUTION Okay... Went back to the first method and made some alterations which ultimately worked...

-(IBAction)sendImage:(id)sender
{
    if(self.secondView == nil)
    {
        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];

        self.secondView = secView;

        secView.theImage = selectedImage.image;
    }

    [self.navigationController pushViewController:secondView animated:YES];
}

And in the secondView, I created UIImage *theImage - then I set [imgView setImage:theImage]; in viewDidLoad, which worked...

3
please put the code of class SecondView - Ganzolo
It's been added now :) But I had already shown the only addition in the code apart from the standard... (please note, the secondView posted is where I try the second method mentioned...) - user969043
Thank you! Passing UIImage vs UIImageView from the original view controller to the secondViewController worked for me as well. Maybe because UIImageView on the secondVC was an IBOutlet it wouldn't work. - LevinsonTechnologies

3 Answers

7
votes

This solved the issue:

-(IBAction)sendImage:(id)sender
{
    if(self.secondView == nil)
    {
        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];

        self.secondView = secView;

        secView.theImage = selectedImage.image;
    }

    [self.navigationController pushViewController:secondView animated:YES];
}

SecondView:

@interface SecondView : UIViewController
{
    UIImageView *imgView;
    UIImage *theImage;
}

@property (nonatomic, retain) IBOutlet UIImageView *imgView;
@property (nonatomic, retain) UIImage *theImage;

-(void)setImage:(UIImage *)image;

@end

and in the .m of secondView:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [imgView setImage:theImage];
}
0
votes

I think I found the problem :

In your class secondView, your UIImageView is not connected to the object UIImageView in the nib file.

You should have something like that :

IBOutlet UIImageView *imgView;

To check if the view is correctly connected to the Nib you can do this :

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [imgView setBackgroundColor:[UIColor redColor]];
    }
    return self;
}

Your problem is interesting please let me know if it doesn't work I'll check deeper.

0
votes

First Viewcontroller:

-(void)passimage
{
    Second_ViewController *select_main = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"Second_ViewController"];
    select_main.tt_image = _bg_imageview.image;
    [[self navigationController]  pushViewController:select_main animated:YES];
}

Second Viewcontroller pass UIImage object:

- (void)viewDidLoad {

    _bg_imageview.image = _bb_image;
}