iphone - 从照片库中选择多张图片

标签 iphone ios xcode ipad

我要问一个可能已经被问过一百万次的问题。

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

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

令我感到沮丧的是,Apple 尚未为我们的开发人员创建内置 API,以实现我们大多数 iPhone/iPad 应用程序中最常请求的功能。 (不是一张而是多选图片)

还有其他示例代码吗?相信我,我已经在谷歌上搜索了一段时间。

最佳答案

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

我的做法是让用户不断选择他们想要的图片,不断将这些文件保存在应用程序文档目录中,直到他们点击完成按钮。在这里查看我的示例代码,希望它能为您省去浏览 Assets 库的痛苦

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

关于iphone - 从照片库中选择多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9542487/

相关文章:

iphone - NSScanner 适用于 WiFi 但不适用于 3G

ios - 模拟器慢动作动画现在打开了吗?

iphone - UITabBarController - 如何为简单的开关设置动画?

iphone - 使用 Wsdl2Objc 的 iPhone 上的 Web 服务?

javascript - 动态插入大量 DOM 元素时,Mobile Safari (iPhone/iPad) 崩溃

ios - 如何将当前日期的 iO​​S 日期格式转换为整数?

ios - 自动设置标签栏按钮和工具栏标题?

iphone - 从不同的 View Controller 访问来自 NSJSONSerialization 的数据

ios - Cocos2d播放2个不同的背景音乐文件或循环播放效果

ios - 导航 Controller popToRootController() 后跟推送 Controller UI 错误