从照片库中选择多个图像 [英] Select Multiple Images from Photo Library

查看:558
本文介绍了从照片库中选择多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问一个问题,或许已经被问了一百万次。

I am going to ask that one question that perhaps has been already asked a million times.

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

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)

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

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.

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

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)

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

Is there any other sample code available? Trust me, I have been googling for a while.

推荐答案

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

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.

我正在做的是让用户继续选择他们想要的图片,将这些文件保存在app documents目录中,直到他们点击完成按钮。请看这里我的示例代码,希望它能为您节省通过资产库的痛苦

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

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

这篇关于从照片库中选择多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆