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

查看:25
本文介绍了从 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.

我希望能够将用户放置到应用程序文档文件夹中的 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天全站免登陆