How to pick multiple images from UIImagePickerController in ios
Solution 1
With UIImagePickerController
you are able to get only one picture. If you need to pick more you need a custom image picker, such as ELCImagePickerController. It works well! You can download it here.
Solution 2
As all said above, it is not possible using just ImagePickerController. You need to do it custom. Apple recently introduced PHASSET Library which makes this easy. There is a sample code also in the developer library. I am laying down the steps here.
- Setup your own collection view
- Load the collection view with pictures from gallery (using PHAsset, explained below)
- Show each of the picture in your cellForItemAtIndexPath (using PHAsset, explained below)
- In your didSelectItemAtIndexPath, keep track of which pictures were selected and add a tick mark image. Add it to a local array
- When done, read from the picture array and process
Snippet code for Loading Images from gallery.
// Create a PHFetchResult object for each section in the table view.
@property (strong, nonatomic) PHFetchResult *allPhotos;
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
if ( _isVideo == YES){
_allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:allPhotosOptions];
}
else {
//_allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
_allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];
}
You now get all the images in your _allPhotos array which you will use like below in the cellForItemAtIndexPath
PHAsset *asset = self.allPhotos[indexPath.item];
//cell.representedAssetIdentifier = asset.localIdentifier;
cell.selectedTick.hidden = YES;
cell.isSelected = NO;
// Request an image for the asset from the PHCachingImageManager.
[self.imageManager requestImageForAsset:asset
targetSize:CGSizeMake(100, 100)
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
cell.photo.image = result;
}];
return cell;
Thatz it. Hope this helps.
user3222991
Updated on November 22, 2020Comments
-
user3222991 over 3 years
I am trying to simply enable picking multiple images from photolibrary using the
UIImagePickerController
.I'm relatively new toXCode
and I don't understand how to allow the user to pick multiple images from theUIImagePickerControler
. This is my current code.Please help any body how to pick multiple images fromUIImagePickerController
.-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch(buttonIndex) { case 0: [self takeNewPhotoFromCamera]; break; case 1: [self choosePhotoFromExistingImages]; default: break; } } - (void)takeNewPhotoFromCamera { if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *controller = [[UIImagePickerController alloc] init]; controller.sourceType = UIImagePickerControllerSourceTypeCamera; controller.allowsEditing = NO; controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera]; controller.delegate = self; [self.navigationController presentViewController: controller animated: YES completion: nil]; } } -(void)choosePhotoFromExistingImages { if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) { UIImagePickerController *controller = [[UIImagePickerController alloc] init]; controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; controller.allowsEditing = NO; controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary]; controller.delegate = self; [self.navigationController presentViewController: controller animated: YES completion: nil]; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [self.navigationController dismissViewControllerAnimated: YES completion: nil]; UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(image, 0.1); } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker; { [self.navigationController dismissViewControllerAnimated: YES completion: nil]; }
-
Kumar KL over 10 yearsCan't do this by Picker. You need to use the custom
-
Kumar KL over 10 yearsIf you just Look in google by "uiimagepickercontroller multiple image selection", You will get lot of possible solutions.
-
Saurav Mac over 10 yearsTry this code stackoverflow.com/questions/9542487/… this may help you .
-
-
Tarun over 10 yearsRight, You need to use a custom image gallery. You may opt to write it yourself using image assest apis or may use any available 3rd party library which fulfills your requirement.
-
user3222991 over 10 years@Euroboy thanks for your response.IS there any alternative to with out using 3rd party framework to do the Multiselection images in UIImagepickercontroller. or told me any other alternative solution for that.
-
iOS Dev over 10 years@user3222991 I already said that with
UIImagePickerController
there is no way to do that. Just tryELCImagePickerController
, it has similar behavior and does exactly what you need. -
NSNoob about 8 yearsYou should add what is
_isVideo
flag info and also the methodrequestImageForAsset
. -
NSNoob about 8 yearsIn any case if anyone is wondering what is
self.manager
, it is a property which can be used likePHImageManager *manager = [PHImageManager defaultManager]
-
Hafeez about 8 yearsSorry, the _isVideo flag is a user set flag based on which the assets are fetched (videos or only photos). Also the "requestImageForAsset" is little tricky since by default it is async and you might get some weird results. I suggest a read [link]developer.apple.com/library/ios/documentation/Photos/Reference/… . There is one more method "requestImageDataForAsset" which is simpler. Hope this helps.
-
Kishore Kumar almost 8 years@Hafeez please add more code it help full to some other :)