Transfer image from one view to another

I get an image from a user from a photo gallery, and I want to transfer it to another view ...

I read that I should just do something like this:

secView.imgView.image = selectedImage.image;

Setting up my UIImageView in the second view as an image from my UIImageView ... But the UIImageView in the second view is just empty ...

My code is ...

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 (relevant parts only)

-(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 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 second view .h

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

and .m

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

But both approaches lead to empty UIImageView ... So what is the correct approach to this?

Thanks in advance!

EDIT

SecondView is pretty simple right now ... (in the published 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

... , ...

-(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 UIImage * theImage - [imgView setImage: theImage]; viewDidLoad, ...

+5
3

:

-(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

.m View:

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

, :

secondView UIImageView UIImageView nib.

- :

IBOutlet UIImageView *imgView;

Nib, :

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

, , , . .

0

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];
}

The second Viewcontroller passes the UIImage object:

- (void)viewDidLoad {

    _bg_imageview.image = _bb_image;
}
0
source

All Articles