UIImagePickerController Pick Multiple images

2019-03-22 04:30发布

问题:

I am trying to simply enable picking multiple images from photolibrary using the UIImagePickerController, I wish I can add a subview on bottom of the photo picker so it look something like this app does:

It there a simple way you can do it? My code currently only pops the images in a standard way but I only dismiss the controller when loaded 6 images

The important thing there is if anyway I can add a small view/toolbar to the photo picker view, like the example did, I then can do the rest

    - (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType{
      //get all available source types
      NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];

      //if source type available and supported media type is not null
      if ([UIImagePickerController isSourceTypeAvailable:sourceType
           && [mediaTypes count] > 0]) {

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //picker.mediaTypes = mediaTypes;   //allow videos
        picker.delegate = self;
        picker.sourceType = sourceType; //set source type to the given type

        /** WANT TO ADD A CUSTOM VIEW IN THE PHOTO PICKER VIEW **/

        [self presentViewController:picker animated:YES completion:nil];
      } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing media"
                                                        message:@"Device doesn't support that media type"
                                                       delegate:nil
                                              cancelButtonTitle:@"Drat !"
                                              otherButtonTitles: nil];
        [alert show];
      }
    }

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
      self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];  //record media type

      //if media type is image
      if ([lastChosenMediaType isEqual:(NSString *) kUTTypeImage]) {
        UIImage *chosenImage = [info objectForKey:UIImagePickerControllerOriginalImage];    //load image

        //save the image if is from camera shot
        if (imageOrVideoSourceType == UIImagePickerControllerSourceTypeCamera) {
          UIImageWriteToSavedPhotosAlbum (chosenImage, nil, nil , nil);
        }

        [images addObject:chosenImage]; //add to image list
        imageNum++;
      }

      changeImageOrVideo = true;

      if(imageNum >= 5){
          [picker dismissViewControllerAnimated:YES completion:nil];
      }
    }

回答1:

The main reason for using hacks like shifting the UIImagePickerController up and showing selected images underneath was because the asset library alternative would involve the user being asked for location access due to the information about where the photo was taken being available in the image metadata.

In iOS 6 the user is asked if they want to allow the app to access their photos (not location) and you get asked this question for both the asset library approach and the UIImagePickerController approach.

As such I think that hacks like the above are nearing the end of their usefulness. Here is a link to a library providing selection of multiple images using the Assets library there are others.