IPhone UIImagePickerController didFinishPickingImage: passing UIImage between view controllers?

In my application, I have a UIImagePickerController. When an image is selected, my view manager needs to get the image and pass it to another view controller, which is placed in self.navigationController. But I always get SEGFAULTS or zero arguments and the like. 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]; } 

Both of them have a current picture, defined as the current picture of UIImage *,
Now I have to have the current picture as some data, but every time it falls! I have tried many different things, and I cannot figure it out.

+4
source share
2 answers

Correct me if I am wrong, but the UIImage does not comply with NSCopying, so you cannot copy it successfully.

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

 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 do this in the dealloc method of the "self" object.

+6
source

One way to exchange data between different classes of class controllers is through the application delegate.

For example, you may have UIImage *currentPicture defined in your application application, and then access it in both controller classes of the form as follows:

 MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication]delegate]; UIImage *i = [appDelegate.currentPicture]; 
-2
source

All Articles