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
{
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) {
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
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, ...