views:

2074

answers:

2

Hello,
In my app, I have a UIImagePickerController. When a image is selected, my view controller needs to get the image and pass it to another view controller, which is pushed onto self.navigationController. But I am always getting SEGFAULTS or nil arguments, and things like that. I would appreciate it if you could tell me what is wrong with this code:

FirstViewController.m:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
 self.currentpicture = [img copy];
 [self dismissModalViewControllerAnimated:YES];
 [self goNext];
}
-(void)goNext{
 SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"Second" bundle:nil];
 [vc giveMePicture:currentpicture];
 [self.navigationController pushViewController:vc animated:YES];
}

SecondViewController.m:

-(void)giveMePicture:(UIImage *)data {
 self.currentpicture=[data copy];
}

They both have currentpicture defined as a UIImage *currentpicture;
I now should have currentpicture as some data, but it crashes every time! I have tried many different things and I cannot figure this out.
I am really inexperienced at this Objective-C - so sorry if this is really obvious.
Isaac Waller

A: 

One way to share data between different view controller classes is through the app delegate.

For example you could have UIImage *currentPicture defined in you app delegate and then access in both of your view controller class as follows:

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication]delegate];
UIImage *i = [appDelegate.currentPicture];
zPesk
+5  A: 

Correct me if I'm wrong, but UIImage does not conform to NSCopying, therefore you can't successfully copy it.

What you probably want to do is retain the image. If self.currentpicture is a 'retain' property, it will automatically release the previous object and retain the new one, so just do this:

self.currentpicture = img;

Otherwise do it yourself:

[self.currentpicture release];
self.currentpicture = [img retain];

In both cases you still have to call [self.currentpicture release] when you no longer need the image. Usually you would do that in the 'self' object's dealloc method.

Chris Lundie