从照片库中选择多个图像

Sam*_*m B 18 iphone xcode ipad ios

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

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

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

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

还有其他示例代码吗?相信我,我一直在谷歌搜索.

Sam*_*m B 20

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

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

 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];
Run Code Online (Sandbox Code Playgroud)

  • 谢谢使用你的代码,我能够自定义uiimagepickerview控制器.现在我想通过图像右上角的samll图像指示用户已经从imagepicker中选择了一张照片.如何做到这一点.任何帮助.. (2认同)

Rav*_*vin 9

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

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

正如文档 Apple所说:

资产库框架

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

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

资产库参考

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