ios 从照片库中选择多个图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9542487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select Multiple Images from Photo Library
提问by Sam B
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. I already have a working code for user to select one image at a time. (not what I need)
我正在为 iPad 制作一个应用程序,并希望让用户能够从他们的照片库中多选图像。我已经有一个工作代码供用户一次选择一个图像。(不是我需要的)
I have already downloaded and looked into ELC image picker sample code but that code is not compatible with iOS 5 or Xcode 4. i.e. 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. (not one but multi-select pics)
我只是感到沮丧的是,苹果还没有为我们的开发人员为我们大多数 iPhone/ipad 应用程序中最常请求的功能创建一个内置 API。(不是一张而是多选图片)
Is there any other sample code available? Trust me, I have been googling for a while.
有没有其他可用的示例代码?相信我,我已经在谷歌上搜索了一段时间。
回答by Sam B
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.
好的,我已经弄清楚了。Assets Library 的问题在于它为您提供了图像的所有 GEO 数据。对于使用您的应用程序的用户来说,这意味着他们会收到一条提示,说明您的应用程序正在尝试访问他们的位置。事实上,您要做的就是让他们从他们的相册中选择多个图像。大多数用户会认为这是盗版问题而被关闭。最好的方法是使用imagePickerController 的apples api。我知道它可以让您一次选择一张图片,但是如果您添加以下代码,它将让您选择多张图片。
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];
回答by Ravin
Apple has provided api for this. It is called ALAssetsLibrary.
苹果为此提供了api。它被称为ALAssetsLibrary。
Using this you can select multiple images/ videos and other operations that you do using photo application on iOS device.
使用它,您可以选择多个图像/视频以及您在 iOS 设备上使用照片应用程序执行的其他操作。
As in documentationApple 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. 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.
资产库框架
在 iOS 4.0 中引入的资产库框架 (AssetsLibrary.framework) 提供了一个基于查询的界面,用于从用户设备检索照片和视频。使用此框架,您可以访问通常由照片应用程序管理的相同资产,包括用户保存的相册中的项目以及导入到设备上的任何照片和视频。您还可以将新照片和视频保存回用户已保存的相册。
Here are few links where you can learn more. Now to use it you can search for ALAssetsLibrary.
这里有几个链接,您可以在其中了解更多信息。现在要使用它,您可以搜索 ALAssetsLibrary。
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/
回答by Mark Krenek
I use ALAssetsLibrary
and rolled my own 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. 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.
我使用ALAssetsLibrary
并推出了自己的用户界面。问题UIImagePickerController
在于它说您应该在didFinishPickingMediaWithInfo
回调中关闭视图控制器,因此通过不关闭来破解多项选择可能会遇到问题。我知道我第一次尝试时做到了。我不记得到底出了什么问题,但有些情况下,UIImagePickerController
如果我没有像文档所说的那样解雇它,就会停止工作。