Select Multiple Images from Photo Library

前端 未结 4 1647
北海茫月
北海茫月 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: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];
    

提交回复
热议问题