从照片库中选择多张图片

20

我要问的问题可能已经被问了无数次。

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

我已经下载并查看了ELC图像选择器示例代码,但该代码与iOS 5或Xcode 4不兼容,即它在ARC和编译问题方面存在问题,并且它在各个地方都使用了release和dealloc。

我只是感到沮丧,因为苹果还没有为我们开发人员创建内置API,以便在我们的iPhone / ipad应用程序中实现这种最常见的功能(不仅仅是多选图片)。

是否有其他示例代码可用?相信我,我已经谷歌搜索了一段时间。

4个回答

22

好的,我已经想通了。Assets Library 的问题在于它会提供图像的所有 GEO 数据。对于使用您的应用程序的用户来说,这意味着他们将收到提示,称您的应用程序正在尝试访问他们的位置。实际上,你只是想让他们从照片库中选择多个图片。大多数用户会认为这是一种盗版问题而感到反感。最好的方法是使用苹果的 imagePickerController api。我知道它可以让你一次选择一张图片,但如果你添加下面的代码,它将让你选择多张图片。

我的做法是让用户继续选择他们想要的图片,将这些文件保存在应用程序文档目录中,直到他们点击完成按钮。 这里是我的示例代码,希望能为您节省浏览 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];

}

//现在使用这段代码获取用户选择的图片。您可以从代码中任意位置调用它。

 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 picker控制器仅用于从照片库中选择多个图像,还是也可以用于私有应用程序目录? - The X-Coder
2
感谢您的代码,我能够自定义uiimagepickerview控制器。现在我想通过在图像右上角放置小图像来指示用户已经从imagepicker选择了照片,如何实现呢?有什么帮助吗? - srividya
你好,你能帮我更新UIImagePickerController中所选图像的设计吗? - Iya

9

苹果已经提供了相应的API,称为ALAssetsLibrary

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

如苹果在文档中所述:

资产库框架

引入于iOS 4.0,资产库框架(AssetsLibrary.framework)提供了一种基于查询的接口,用于从用户的设备检索照片和视频。使用此框架,您可以访问通常由照片应用程序管理的相同资产,包括用户保存的照片相册中的项目以及导入到设备上的任何照片和视频。您还可以将新照片和视频保存回用户的已保存照片相册。

以下是一些可以了解更多信息的链接。现在,要使用它,您可以搜索ALAssetsLibrary。

资产库参考

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


1
资产框架已被弃用,请改用照片框架。 - Vinoth Vino

2
从iOS 14开始,在新的照片选择器中支持多图选择。下面是来自Apple的示例代码:在iOS中选择照片和视频。请注意保留HTML标签。

这很棒,但它只支持iOS 14+ :[ - self.name

1

我使用了ALAssetsLibrary并自己编写了UI。使用UIImagePickerController的问题在于它说你应该在didFinishPickingMediaWithInfo回调中解除视图控制器,因此通过不解除选择多个项目可能会遇到问题。当我第一次尝试时,我知道我做错了。我记不清楚出了什么问题,但是如果我不像文档中所说那样解除它,有些情况下UIImagePickerController就会停止工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接