从照片库中select多个图像

我想问一个问题,也许已经有一百万次了。

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

我已经下载并查看了ELC图像select器示例代码,但是该代码与iOS 5或Xcode 4不兼容。即它具有ARC并且左右编译问题,其使用版本和dealloc遍布各处。

我只是感到沮丧,苹果还没有为我们的开发人员在大多数iPhone / iPad应用程序中最常用的function创build一个内置的API。 (不是一个,但多选图片)

还有其他示例代码吗? 相信我,我一直在Google上search。

好的,我有这个想法。 Assets Library的问题在于它提供了图像的所有GEO数据。 这意味着你的用户使用你的应用程序的意思是,他们会得到一个提示,说你的应用程序正在尝试访问他们的位置。 事实上,你所要做的就是让他们从他们的相册中select多个图像。 大多数用户会被认为是盗版问题。 最好的方法是使用imagePickerController的苹果api。 我知道它可以让你一次select一张照片,但如果你添加下面的代码,它会让你select多张照片。

我正在做的方式是让用户不断select他们想要的图片,不断保存这些文件在应用程序文档目录中,直到他们点击完成button。 在这里看到我的示例代码,并希望它会为您节省通过资产库的痛苦

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

//现在使用这个代码来获取用户select的图片。 从代码中的任何位置调用它

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

苹果已经为此提供了API。 它被称为ALAssetsLibrary 。

使用此function,您可以select在iOS设备上使用照片应用程序进行的多个图像/video和其他操作。

正如苹果在文件中所说:

资产库框架

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

这里有几个链接,你可以了解更多。 现在使用它可以searchALAssetsLibrary。

资源库参考

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

我使用ALAssetsLibrary并推出了我自己的用户界面。 UIImagePickerController的问题是,它说你应该closuresdidFinishPickingMediaWithInfocallback中的视图控制器,所以通过不closures来攻击多个select可能会遇到问题。 当我第一次尝试时我知道我做了。 我不记得究竟是哪里出了问题,但有些情况下, UIImagePickerController只是停止工作,如果我没有像文档说的那样解雇它。