vinay chorpa
vinay chorpa

Reputation: 187

selecting image for multiple image view from cam or library

I have 3 Image views on my view controller (Purpose Before ,During, After) . i need to select images for each of view respectively .

the didicated method for this task is :

 -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
      UIImage *image;
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [_imgView setImage:image];
        [self dismissViewControllerAnimated:YES completion:NULL];
}

but how to notify this method about which button has called the image picker and to select/display the image on view respectively .

Upvotes: 1

Views: 637

Answers (2)

Paul.s
Paul.s

Reputation: 38728

Add a "private" (yes I know no such thing) ivar to hold a reference to the selectedImageView

@interface MyViewController ()

@property (nonatomic, strong) UIImageView *selectedImageView;

@end

Then when the imageView is selected use this new ivar to keep a reference to the currently active imageView

- (void)imageViewTapped:(id)sender
{
  // This assumes the use of a gesture recognizer but you can substitute your usual method
  // of detecting the imageView that you want to edit.
  self.selectedImageView = [sender view];
}

Now your delegate can look like this

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  self.selectedImageView.image = info[UIImagePickerControllerOriginalImage];
  self.selectedImageView = nil;

  [self dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 2

Apurv
Apurv

Reputation: 17186

On a class level take a variable that holds the reference of the imageview.

When you are tapping the image, store the tapped imageview's reference into this variable.

Into imagepicker delegate method, use this variable to set image.

Upvotes: 0

Related Questions