UIImagePickerController,将2个图像保存到不同名称的documentsdir

Posted

技术标签:

【中文标题】UIImagePickerController,将2个图像保存到不同名称的documentsdir【英文标题】:UIImagePickerController, saveing 2 images to documentsdir with different names 【发布时间】:2014-01-15 09:37:30 【问题描述】:

我想保存 2 张图片,因为它们被挑选为不同的名称。

我使用本指南制作了我的 uiimagepickercontroller:guide

基本上它是一个管理 2 个 uiimageviews 的 uiimagepickercontroller,是否可以让它保存 2 个不同名称的不同图像?还是我应该尝试让 uiimagepickercontroller 与众不同?

这是我的代码,它不能用于保存。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];

//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"])
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *webData = UIImagePNGRepresentation(editedImage);
    [webData writeToFile:imagePath atomically:YES];

switch (_selectedPhotoType) 
    case imageView1Type:
        imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        break;
    case imageView2Type:
        imageView2.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        break;
    default:
        break;


[self dismissViewControllerAnimated:YES completion:^ ];

【问题讨论】:

所以您想将相同的选取图像保存到 2 个不同的文件中? 简单的问题,你想要一个按钮和两个选择器还是两个按钮和一个选择器..... @heximal 不,我有 2 个 uiimageviews,当我单击每个图像下方的按钮时,我选择了 2 个不同的图像...我想将它们保存在每个不同的名称下 【参考方案1】:

看看这个实现。那是你需要的吗?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];

        //extracting image from the picker and saving it
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
        NSData *webData = UIImagePNGRepresentation(editedImage);
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        switch (_selectedPhotoType) 
            case imageView1Type: 
                NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo1.png"];
                imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
                break;
            case imageView2Type: 
                NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo2.png"];
                imageView2.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
                break;
            default:
                break;
        
        [webData writeToFile:imagePath atomically:YES];

        [self dismissViewControllerAnimated:YES completion:^ ];
        

【讨论】:

【参考方案2】:

在@Heximal 的帮助下解决了这个问题

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];

//extracting image from the picker and saving it
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
switch (_selectedPhotoType) 
    case imageView1Type: 
        NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo1.png"];
        imageView1.image = [info objectForKey:UIImagePickerControllerEditedImage];
        if ([mediaType isEqualToString:@"public.image"])
            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
            NSData *webData = UIImagePNGRepresentation(editedImage);
            [webData writeToFile:imagePath atomically:YES];
        

    
        break;
    case imageView2Type: 
        NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo2.png"];
        imageView2.image = [info objectForKey:UIImagePickerControllerEditedImage];
        if ([mediaType isEqualToString:@"public.image"])
            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
            NSData *webData = UIImagePNGRepresentation(editedImage);
            [webData writeToFile:imagePath atomically:YES];
        
    
        break;
    default:
        break;


[self dismissViewControllerAnimated:YES completion:^ ];

【讨论】:

以上是关于UIImagePickerController,将2个图像保存到不同名称的documentsdir的主要内容,如果未能解决你的问题,请参考以下文章

将 UIImagePickerController 与 Web 数据源一起使用

将图像加载到本机 UIImagePickerController 允许编辑

UIImagePickerController,将2个图像保存到不同名称的documentsdir

有没有办法将 UIImagePickerController 与您自己的图像一起使用?

关闭 UIImagePickerController 将 UITabBarController 重置为 0 索引 - iOS

将 UIView 覆盖添加到 UIIMagePickerController 视频录制