【问题标题】:Select Multiple Images from Photo Library从照片库中选择多个图像
【发布时间】:2012-03-21 11:58:12
【问题描述】:

我要问一个可能已经被问过一百万次的问题。

我正在为 iPad 制作一个应用程序,并希望让用户能够从他们的照片库中多选图像。我已经有一个工作代码供用户一次选择一个图像。 (不是我需要的)

我已经下载并查看了 ELC 图像选择器示例代码,但该代码与 iOS 5 或 Xcode 4 不兼容。即它有 ARC 和左右编译问题,它到处使用 release 和 dealloc。

我很沮丧,苹果还没有为我们的开发人员创建一个内置 api,用于我们大多数 iPhone/ipad 应用程序中最常请求的功能。 (不是一张,而是多选图片)

还有其他可用的示例代码吗?相信我,我已经在谷歌上搜索了一段时间。

【问题讨论】:

    标签: iphone ios xcode ipad


    【解决方案1】:

    好的,我想通了。资产库的问题在于它为您提供了图像的所有 GEO 数据。对于使用您的应用程序的用户来说,这意味着他们将收到一条提示,说明您的应用程序正在尝试访问他们的位置。事实上,你要做的就是让他们从他们的相册中选择多张图片。大多数用户会认为这是盗版问题而被关闭。最好的方法是使用 imagePickerController 的 apples api。我知道它可以让您一次选择一张图片,但是如果您添加以下代码,它将让您选择多张图片。

    我正在做的方式是让用户继续选择他们想要的图片,继续将这些文件保存在应用程序文档目录中,直到他们点击完成按钮。在这里查看我的示例代码,希望它可以为您省去浏览资产库的痛苦

    -(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];
    
    }
    

    //现在使用此代码获取用户选择的图片。从代码中的任何位置调用它

     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];
    

    【讨论】:

    • 嗨...我不想从照片库中选择图片,而是从私有应用目录中选择图片。我的问题是 ELCImage 选择器控制器仅用于从照片库中选择多个图像,或者它也可以用于私有应用程序目录?
    • 感谢使用您的代码,我能够自定义 uiimagepickerview 控制器。现在我想通过图像右上角的 samll 图像指示用户已经从图像选择器中选择了一张照片。任何帮助..
    • 您好,您能帮我更新 UIImagePickerController 中所选图像的设计吗?
    【解决方案2】:

    Apple 为此提供了 api。它被称为ALAssetsLibrary

    使用它,您可以选择多个图像/视频以及您在 iOS 设备上使用照片应用程序执行的其他操作。

    正如documentation Apple 所说:

    资产库框架

    在 iOS 4.0 中引入的 Assets Library 框架 (AssetsLibrary.framework) 提供了一个基于查询的接口 从用户的设备中检索照片和视频。使用这个 框架,您可以访问通常由 照片应用程序,包括用户保存的照片中的项目 相册以及导入设备的所有照片和视频。 您还可以将新照片和视频保存回用户的已保存 相册。

    这里有几个链接,您可以从中了解更多信息。现在要使用它,您可以搜索 ALAssetsLibrary。

    Assets Library Reference

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

    【讨论】:

    • 资产框架已弃用。改用照片框架
    【解决方案3】:

    从 iOS 14 开始,新的照片选择器现在支持多张图片选择。这是来自 Apple 的示例代码:Selecting Photos and Videos in iOS

    【讨论】:

    • 这很棒,但它的 iOS 14+ :[
    【解决方案4】:

    我使用 ALAssetsLibrary 并推出了自己的 UI。 UIImagePickerController 的问题在于它说您应该在 didFinishPickingMediaWithInfo 回调中关闭视图控制器,因此通过不关闭来破解多项选择可能会遇到问题。我知道当我第一次尝试时我做到了。我不记得到底出了什么问题,但在某些情况下,UIImagePickerController 如果我没有像文档说的那样将其关闭,就会停止工作。

    【讨论】:

      猜你喜欢
      • 2011-06-12
      • 2017-09-06
      • 2012-03-06
      • 1970-01-01
      • 2016-07-13
      • 2018-10-04
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多