1

I am brand new to using UIImagePicker so bear with me here. Basically, I have two UIButtons defined that access the camera roll and once an image is chosen, the image appears in my UIImageView. The problem is when I choose an image from one of the buttons, it appears in both UIImageViews, and not one. I basically want to be able to choose two different photos by means of two different buttons. Here is the coding:

-(IBAction)getPhoto1:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; popover1 = [[UIPopoverController alloc] initWithContentViewController:picker]; [popover1 presentPopoverFromRect:CGRectMake(0.0, 0.0, 400., 400.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } -(IBAction)getPhoto2:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; popover2 = [[UIPopoverController alloc] initWithContentViewController:picker]; [popover2 presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:nil]; photo1.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; photo2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; } 

1 Answer 1

2

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info is called whenever you finish selecting an image.

When the you finish selecting an image from the UIImagePickerController, you are setting both UIImageView to the image returned.

You need to differentiate between which button has been pressed - perhaps you could use a int to determine which button was pressed.

For example, create an 'int' called buttonPressed that'll be set to 1 when getPhoto1 was pressed, or 2 when getPhoto2 was pressed. Then, in didFinishPickingMediaWithInfo, you could have:

if (buttonPressed == 1) { photo1.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; } else if (buttonPressed == 2) { photo2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this ended up working great. I set an int above the actions: int buttonSelect = 0; Then set it to 1 and 2 in the different actions and it worked great. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.