从IOS上的UIView将图像保存到应用程序文档文件夹 [英] Save An Image To Application Documents Folder From UIView On IOS

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

问题描述

我有一个UIImageView,它允许用户放置并保存图像,直到可以保存为止.问题是,我不知道如何实际保存和检索放置在视图中的图像.

I have a UIImageView that allows a user to place and hold an image until it can be saved. The problem is, I can't figure out how to actually save and retrieve the image I've placed in the view.

我已经像这样检索图像并将其放置在UIImageView中:

I have retrieved and placed the image in the UIImageView like this:

//Get Image 
- (void) getPicture:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = (sender == myPic) ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    myPic.image = image;
    [picker dismissModalViewControllerAnimated:YES];
}

它在我的UIImageView中显示选定的图像就很好了,但是我不知道如何保存它.我将视图的所有其他部分(主要是UITextfield)保存在Core Data中.我已经搜索了很多,然后尝试了很多人建议的代码,但是要么我没有正确输入代码,要么这些建议与我的代码设置方式不符.可能是前者.我想使用与将文本保存在UITextFields中相同的操作(保存按钮)将图像保存在UIImageView中.这是我保存UITextField信息的方式:

It displays the selected image in my UIImageView just fine, but I have no idea how to save it. I'm saving all the other pieces of the view (mostly UITextfield) in Core Data. I've searched and searched, and tried many bits of code that people have suggested, but either I'm not entering the code correctly, or those suggestions don't work with the way I have my code set up. It's likely the former. I'd like to save the image in the UIImageView using the same action (a save button) I'm using to save the text in the UITextFields. Here's how I'm saving my UITextField info:

// Handle Save Button
- (void)save {

    // Get Info From UI
    [self.referringObject setValue:self.myInfo.text forKey:@"myInfo"];

就像我之前说过的那样,我尝试了几种方法来使它起作用,但是无法掌握它.我一生中第一次想对无生命的物体造成身体伤害,但我设法克制了自己.

Like I said earlier, I have tried several methods to get this to work, but can't get a grasp on it. For the first time in my life I've wanted to cause physical harm to an inanimate object, but I've managed to restrain myself.

我希望能够将用户放置的图像保存到应用程序的documents文件夹中的UIImageView中,然后能够检索它并将其放置在另一个UIImageView中,以便在用户将该视图推入堆栈时显示.任何帮助,我们将不胜感激!

I'd like to be able to save the image the user places into the UIImageView in the application's documents folder, and then be able to retrieve it and place it in another UIImageView for display when the user pushes that view onto the stack. Any help is greatly appreciated!

推荐答案

这一切都很好,伙计.不要伤害自己或他人.

It's all good, man. Don't harm yourself or others.

您可能不想将这些图像存储在Core Data中,因为如果数据集变得太大,这会影响性能.最好将图像写入文件.

You probably don't want to store these images in Core Data, since that can impact performance if the data set grows too large. Better to write the images to files.

NSData *pngData = UIImagePNGRepresentation(image);

这会提取您捕获的图像的PNG数据.从这里,您可以将其写入文件:

This pulls out PNG data of the image you've captured. From here, you can write it to a file:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file

以后阅读它的方式相同.像上面一样构建路径,然后:

Reading it later works the same way. Build the path like we just did above, then:

NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:pngData];

您可能想要做的是创建一个为您创建路径字符串的方法,因为您不希望该代码随处可见.可能看起来像这样:

What you'll probably want to do is make a method that creates path strings for you, since you don't want that code littered everywhere. It might look like this:

- (NSString *)documentsPathForFileName:(NSString *)name
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0];

    return [documentsPath stringByAppendingPathComponent:name]; 
}

希望有帮助.

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

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