在NSUserDefaults中保存图像

时间:2014-05-20 18:13:12

标签: ios objective-c image nsuserdefaults

我需要在我的应用中存储和检索图像,我首先想到这样做:

[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image)            forKey:key];

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:key];
UIImage* image = [UIImage imageWithData:imageData];

但我读到这不是推荐的方式。对此最好的方法是什么?

(我知道这个问题有重复,但我还没有明白该怎么做)

4 个答案:

答案 0 :(得分:3)

存储图片的最佳方式取决于您的应用的功能。

如果您有100k +图像,您可能希望手动将其保存到iphone的硬盘上,因为它会以这种方式加载图像。

但是,如果你的数量少于此数,那么我将推荐将图像作为二进制数据存储在核心数据中。

使用coredata与文件系统的好处:

  • 更好的iCloud Sync支持
  • 无需管理硬盘上的图像以及用作“持久性存储”的图像。 (coredata / NSUserDefaults的/定制的)
  • 您可以将图像与其他数据联系起来,例如姓名,创建日期等。

文件系统与coredata的一些有趣的性能信息:http://biasedbit.com/filesystem-vs-coredata-image-cache/

答案 1 :(得分:1)

您必须从UIImageJPEGRepresentation()获取数据并将其存储在NSData对象或某些对象中,以便将其“plist-serializable”存储在NSUserDefaults中,但正如其他人所说,你最好将图像作为图像文件存储在文件系统的某个地方,并将文件路径或文件URL存储在NSUserDefaults中。

答案 2 :(得分:0)

将图像保存在Documents目录中,然后将文件名保存在数据库(可能是Core Data)中,另一种文件格式或NSUserDefaults

NSUserDefaults并不是存储应用数据的好方法。

从UIImage实例获取数据 UIImagePNGRepresentation()或UIImageJPEGRepresentation()。然后将数据保存到文件中。使用imageWithContentsOfFile:恢复UIImage。

获取文档目录的路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];

答案 3 :(得分:0)

最简单的方法是直接将内容保存到iOS设备并将路径存储在NSUserDefaults中。

首先将图像转换为文件并将其保存到手机中。

UIImage *image = // your image
NSData *pngImageData = UIImagePNGRepresentation(image);

NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];

NSString *fileName = [NSString stringWithFormat:@"%@/imageName.png",
                      documentsDirectory];
[pngImageData writeToFile:fileName atomically:YES];
由于在NSUserDefaults中存储了filePaths,我的同事威胁要投票给我,他反对这一点,因为你不想在NSUserDefaults中存储任何超级动态的东西,你可能会存储像会话密钥这样的东西,或者令牌值,用户名甚至,不经常更改或非常严格的事情。

如果您绝对拒绝CoreData,建议是创建属性列表并在那里存储字符串名称。

要将imagePath存储在plist中,您可以执行以下操作:

NSString *textPath = [[NSBundle mainBundle] pathForResource:@"propertyListName" ofType:@"plist"];
NSDictionary *thisDict = [NSDictionary dictionaryWithContentsOfFile:textPath];
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:thisDict];
mutableDictionary[@"key"] = fileName;
[mutableDictionary writeToFile:resultsPath atomically:YES];

要从plist中检索图像,您可以执行以下操作

NSString *textPath = [[NSBundle mainBundle] pathForResource:@"propertyListName" ofType:@"plist"];
NSDictionary *thisDict = [NSDictionary dictionaryWithContentsOfFile:textPath];
NSString *imagePath = thisDict[@"key"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
UIImage *myImage = [UIImage imageWithData:imageData];

不要使用此NSUserDefaults选项,因为它是错误的,但如果你愿意,这就是你要做的。

[[NSUserDefaults standardUserDefaults] setObject:fileName forKey:@"key"];
[[NSUserDefaults standardUserDefaults] synchronize];

现在它很容易保存以供您检索。当您想要检索图像时。

NSString *filePath = [[NSUserDefaults standardUserDefaults] objectForKey:@"key"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
UIImage *myImage = [UIImage imageWithData:imageData];

然后使用myImage作为图像。请记住,设置和检索时密钥必须相同。

存储图片的最佳方式取决于您的应用的功能。

如果您有100k +图像,您可能希望通过coredata手动将其保存到iphone的硬盘上,因为它会以这种方式加载图像。

但是,如果你的数量少于此数,那么我将推荐将图像作为二进制数据存储在核心数据中。

使用coredata与文件系统的好处:

  • 更好的iCloud Sync支持
  • 无需管理硬盘上的图像以及用作“持久性存储”的内容(coredata / nsuserdefaults / custom)
  • 您可以将图像与其他数据联系起来,例如姓名,创建日期等。

文件系统与coredata的一些有趣的性能信息:http://biasedbit.com/filesystem-vs-coredata-image-cache/