2

I have 3 image views on my view controller. I have an option like choose from gallery or take a picture. Now, what I want is from the camera. I want to pick images by default, we can pick one, but I want three how can we achieve it.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
praveen kumar
  • 141
  • 10
  • 1
    Did you check for existing answers in the community ? http://stackoverflow.com/questions/20756899/how-to-select-multiple-images-from-uiimagepickercontroller – Bharath Jul 27 '16 at 07:25
  • UIImagePickerController doesn't allow you to select multiple images. You could try a custom ImagePickerController such as this one: https://github.com/B-Sides/ELCImagePickerController but it might not work as expected as the class this library is built on (ALAssetsLibrary) has been deprecated as of iOS 9. – MShahmeer Jul 27 '16 at 07:26
  • `we can pick one, but I want three how can we achieve it` can you explain these line in brief – Anbu.Karthik Jul 27 '16 at 07:44

3 Answers3

3

use Tag Concept`

Step-1

Initially assign the tag for your each imageView as 1,2,3 and crete the one common method for open the UIImagePickerController

Step-2

create the gesture for each image

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
 imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tap];

call method like

- (IBAction)handleImageTap:(UIGestureRecognizer*)sender

{

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * action) {
                                                            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                                                            {
                                                                UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                                picker.view.tag = sender.view.tag;
                                                                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                                                                picker.delegate = self;
                                                                picker.allowsEditing = YES;
                                                                [self presentViewController:picker animated:YES completion:NULL];
                                                            }

                                                        }];
UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                         picker.view.tag = sender.view.tag;
                                                         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                                                         picker.delegate = self;
                                                         picker.allowsEditing = YES;
                                                         [self  presentViewController:picker animated:YES completion:NULL];
                                                     }];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
                                                 style:UIAlertActionStyleCancel
                                               handler:^(UIAlertAction * action) {
                                               }];

[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:NULL];

}

Step-3

on that delegate method

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
if(picker.view.tag == 1)
{
 // assign the image to first
}
else if(picker.view.tag == 2)
{
 // assign the image to second
}
else
{

// assign the image to third
}

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

You can write your own logic something like,

In didFinishPickingImage increment one counter and don't dismiss image picker control until counter reaches at 3 and store image every time n local storage like in document directory or temp directory.

Refer accepted answer of this so post for demonstration of what i have explained.

If you want to use third party library then ELCImagePickerController is better solution.

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

I recommended two Third-party to you to pick multiple images and deal with to as you can :-

1-ELCImagePickerController

2-CTAssetsPickerController

Ahmed Abdallah
  • 2,338
  • 1
  • 19
  • 30