简体   繁体   English

从照片库中选择多个图像

[英]Select Multiple Images from Photo Library

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-select images from their photo-library.我正在为 iPad 制作一个应用程序,并希望让用户能够从他们的照片库中多选图像。 I already have a working code for user to select one image at a time.我已经有一个工作代码供用户一次选择一个图像。 (not what I need) (不是我需要的)

I have already downloaded and looked into ELC image picker sample code but that code is not compatible with iOS 5 or Xcode 4. ie it has ARC and compile problems left and right, its using release and dealloc all over the place.我已经下载并查看了 ELC 图像选择器示例代码,但该代码与 iOS 5 或 Xcode 4 不兼容。即它有 ARC 和左右编译问题,它到处都是使用 release 和 dealloc。

I am just frustrated that apple hasn't already created a built in-api for us developers for this most commonly requested functionality in most of our iPhone/ipad apps.我只是很沮丧,苹果还没有为我们的开发人员创建一个内置的 api,用于我们大多数 iPhone/ipad 应用程序中最常请求的功能。 (not one but multi-select pics) (不是一张而是多选图片)

Is there any other sample code available?有没有其他可用的示例代码? Trust me, I have been googling for a while.相信我,我已经在谷歌上搜索了一段时间。

Ok, I have this figured out.好的,我已经弄清楚了。 The problem with Assets Library is that it gives you all the GEO data of the image. Assets Library 的问题在于它为您提供了图像的所有 GEO 数据。 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.最好的方法是使用imagePickerController 的apples api。 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在这里查看我的示例代码,希望它可以为您省去通过 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];

Apple has provided api for this.苹果为此提供了api。 It is called ALAssetsLibrary .它被称为ALAssetsLibrary

Using this you can select multiple images/ videos and other operations that you do using photo application on iOS device.使用它,您可以选择多个图像/视频以及您在 iOS 设备上使用照片应用程序执行的其他操作。

As in documentation Apple says:正如 Apple 在文档中所说:

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.在 iOS 4.0 中引入的资产库框架 (AssetsLibrary.framework) 提供了一个基于查询的界面,用于从用户设备检索照片和视频。 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.现在要使用它,您可以搜索 ALAssetsLibrary。

Assets Library Reference资产库参考

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

Starting with iOS 14, multiple image selection is now supported in the new Photos picker.从 iOS 14 开始,新的照片选择器现在支持多张图片选择。 Here is sample code from Apple: Selecting Photos and Videos in iOS .这是来自 Apple 的示例代码: 在 iOS 中选择照片和视频

I use ALAssetsLibrary and rolled my own UI.我使用ALAssetsLibrary并推出了我自己的 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. UIImagePickerController的问题在于它说您应该在didFinishPickingMediaWithInfo回调中关闭视图控制器,因此通过不关闭来破解多个选择可能会遇到问题。 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.我不记得到底出了什么问题,但有些情况下UIImagePickerController如果我没有像文档所说的那样关闭它,就会停止工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM