Select Multiple Images from Photo Library

前端 未结 4 1609
北海茫月
北海茫月 2020-12-02 06:49

I am going to ask that one question that perhaps has been already asked a million times.

I am making an app for iPad and want to give users the ability to multi-sel

相关标签:
4条回答
  • 2020-12-02 07:31

    Starting with iOS 14, multiple image selection is now supported in the new Photos picker. Here is sample code from Apple: Selecting Photos and Videos in iOS.

    0 讨论(0)
  • 2020-12-02 07:41

    Ok, I have this figured out. The problem with Assets Library is that it gives you all the GEO data of the image. What that means for your users using your app is that they will get a prompt saying that your app is trying to access their location. Infact all you are trying to do is let them choose multiple images from their photo-album. Most users will be turned off thinking its a piracy issue. The best approach is to use apples api of imagePickerController. I know it lets you choose one pic at a time but if you add the following code, it will let you choose multiple pictures.

    The way I am doing is let the users keep selecting pictures they want, keep saving those files in the app documents directory, till they hit the done button. See here my sample code and hopefully it will save you the pain of going through Assets Library

    -(IBAction)selectExitingPicture
    {
        //Specially for fing iPAD
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
    
        popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
        [popoverController presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 300.0) 
                                 inView:self.view
               permittedArrowDirections:UIPopoverArrowDirectionAny 
                               animated:YES];
    }
    
    //Done button on top
    - (void)navigationController:(UINavigationController *)navigationController
          willShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {    
        //NSLog(@"Inside navigationController ...");
    
    
        if (!doneButton) 
        {
            doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                          style:UIBarButtonItemStyleDone
                                                         target:self action:@selector(saveImagesDone:)];
        }
    
        viewController.navigationItem.rightBarButtonItem = doneButton;
    }
    
    - (IBAction)saveImagesDone:(id)sender
    {
        //NSLog(@"saveImagesDone ...");
    
        [popoverController dismissPopoverAnimated:YES];
    }
    
    
    -(void)imagePickerController:(UIImagePickerController *)picker
          didFinishPickingImage : (UIImage *)image
                     editingInfo:(NSDictionary *)editingInfo
    {
    
    
        //DONT DISMISS
        //[picker dismissModalViewControllerAnimated:YES];
        //[popoverController dismissPopoverAnimated:YES];
    
            IMAGE_COUNTER = IMAGE_COUNTER + 1;
    
            imageView.image = image;
    
            // Get the data for the image
            NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
    
    
            // Give a name to the file
            NSString* incrementedImgStr = [NSString stringWithFormat: @"UserCustomPotraitPic%d.jpg", IMAGE_COUNTER];
    
    
            NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString* documentsDirectory = [paths objectAtIndex:0];
    
            // Now we get the full path to the file
            NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr];
    
            // and then we write it out
            [imageData writeToFile:fullPathToFile2 atomically:NO];
    
    }
    

    //Now use this code to get to user selected pictures. Call it from wherever you want in your code

     NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
            NSString* documentsPath = [paths objectAtIndex:0];
            NSString* dataFile = [documentsPath stringByAppendingPathComponent:@"UserCustomPotraitPic1.jpg"];
    
            NSData *potraitImgData = [NSData dataWithContentsOfFile:dataFile];
            backgroundImagePotrait = [UIImage imageWithData:potraitImgData];
    
    0 讨论(0)
  • 2020-12-02 07:48

    Apple has provided api for this. It is called ALAssetsLibrary.

    Using this you can select multiple images/ videos and other operations that you do using photo application on iOS device.

    As in documentation Apple says:

    Assets Library Framework

    Introduced in iOS 4.0, the Assets Library framework (AssetsLibrary.framework) provides a query-based interface for retrieving photos and videos from the user’s device. Using this framework, you can access the same assets that are normally managed by the Photos application, including items in the user’s saved photos album and any photos and videos that were imported onto the device. You can also save new photos and videos back to the user’s saved photos album.

    Here are few links where you can learn more. Now to use it you can search for ALAssetsLibrary.

    Assets Library Reference

    http://www.fiveminutes.eu/accessing-photo-library-using-assets-library-framework-on-iphone/

    0 讨论(0)
  • 2020-12-02 07:52

    I use ALAssetsLibrary and rolled my own UI. The problem with UIImagePickerController is that it says you are supposed to dismiss the view controller in the didFinishPickingMediaWithInfo callback, so hacking multiple selection by not dismissing may run into problems. I know I did when I first tried it. I can't recall exactly what went wrong, but there were cases where UIImagePickerController simply stopped working if I didn't dismiss it like the docs say.

    0 讨论(0)
提交回复
热议问题