如何从UIImagePickerController将图像保存到应用程序文件夹? [英] How to Save Image to Application Folder from UIImagePickerController?

查看:156
本文介绍了如何从UIImagePickerController将图像保存到应用程序文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,让我发疯。我已经搜索了很多东西来解决它,但我发现的所有答案都很老,而且没有帮助。

I've a question in mind that driving me crazy. I've searched a lot to solve it, but all the answers that I found were old and not helped.

我正在使用 UIImagePickerControllerDelegate 进行应用,并使用 didFinishPickingMediaWithInfo 我要保存所选图像到应用程序文件夹。

I'm currently working on application with that uses UIImagePickerControllerDelegate and with didFinishPickingMediaWithInfo I want to save the selected image to application folder.

到现在为止,我可以用这个选择图像;

Until now I'm able to select the image with this;

- (IBAction)btnPressed:(UIButton *)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
        imgPicker.delegate = self;
        imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imgPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
        imgPicker.allowsEditing = NO;

        [self presentViewController:imgPicker animated:YES completion:nil];
    }

}

但无法将其保存到应用程序使用didFinishPickingMediaWithInfo;

but couldn't save it to application using didFinishPickingMediaWithInfo;

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

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([mediaType isEqualToString:(NSString *) kUTTypeImage])
    {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

        //Assigning the selected image to imageView to see on UI side.
        imageViewerImage.image = image;
    }
}

我想知道在这之后应该是什么部分。

I wonder what should be the part after this point.

感谢您提供的任何帮助。

I appreciate any help that you can provide.

推荐答案

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

    [self dismissViewControllerAnimated:YES completion:NULL];

UIImage* image;

if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:@"public.image"])
    {

  image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];

 NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];

// New Folder is your folder name

 NSError *error = nil;

if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];

NSData *data = UIImageJPEGRepresentation(image, 1.0);

[data writeToFile:fileName atomically:YES];

   }
}

这篇关于如何从UIImagePickerController将图像保存到应用程序文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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