IPhone UIImagePickerController didFinishPickingImage: UIImage transfer between view controllers

iphoneuiimageuiimagepickercontrolleruiviewcontroller

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.

Best Solution

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.

Related Question