在NSUserDefaults中保存图像? [英] Save images in NSUserDefaults?
问题描述
是否可以将图像保存为 NSUserDefaults
作为对象,然后检索以供进一步使用?
Is it possible to save images into NSUserDefaults
as an object and then retrieve for further use?
推荐答案
注意!如果您在iOS8 / XCODE6下工作,请查看我的更新
对于那些仍在寻找答案的人来说,这是保存图像的可取方式的代码在NSUserDefaults中。您不应该将图像数据直接保存到NSUserDefaults中!
For those who still looking for answer here is code of "advisable" way to save image in NSUserDefaults. You SHOULD NOT save image data directly into NSUserDefaults!
写入数据:
// Get image data. Here you can use UIImagePNGRepresentation if you need transparency
NSData *imageData = UIImageJPEGRepresentation(image, 1);
// Get image path in user's folder and store file with name image_CurrentTimestamp.jpg (see documentsPathForFileName below)
NSString *imagePath = [self documentsPathForFileName:[NSString stringWithFormat:@"image_%f.jpg", [NSDate timeIntervalSinceReferenceDate]]];
// Write image data to user's folder
[imageData writeToFile:imagePath atomically:YES];
// Store path in NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:imagePath forKey:kPLDefaultsAvatarUrl];
// Sync user defaults
[[NSUserDefaults standardUserDefaults] synchronize];
读取数据:
NSString *imagePath = [[NSUserDefaults standardUserDefaults] objectForKey:kPLDefaultsAvatarUrl];
if (imagePath) {
self.avatarImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];
}
documentsPathForFileName:
- (NSString *)documentsPathForFileName:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
return [documentsPath stringByAppendingPathComponent:name];
}
适用于iOS8 / XCODE6
As下面的评论中提到的tmr和DevC存在xcode6 / ios8的问题。 xcode5和xcode 6安装过程之间的区别在于xcode6 在xcode中每次运行后更改应用程序UUID (请参阅路径中的高亮部分:/ var / mobile / Containers / Data / Application / B0D49CF5- 8FBE-4F14-87AE-FA8C16A678B1 /Documents/image.jpg)。
For iOS8/XCODE6 As tmr and DevC mentioned in comments below there is a problem with xcode6/ios8. The difference between xcode5 and xcode 6 installation process is that xcode6 changes apps UUID after each run in xcode (see hightlighted part in path: /var/mobile/Containers/Data/Application/B0D49CF5-8FBE-4F14-87AE-FA8C16A678B1/Documents/image.jpg).
所以有2种解决方法:
- 跳过这个问题,因为一旦安装在真实设备上的应用程序,它永远不会改变UUID(事实上它确实如此,但它是新的应用程序)
- 保存所需文件夹的相对路径(在我们的例子中是应用程序的根目录)
这是快速版本的代码作为奖励(第二种方法) ):
Here is swift version of code as a bonus (with 2nd approach):
写入数据:
let imageData = UIImageJPEGRepresentation(image, 1)
let relativePath = "image_\(NSDate.timeIntervalSinceReferenceDate()).jpg"
let path = self.documentsPathForFileName(relativePath)
imageData.writeToFile(path, atomically: true)
NSUserDefaults.standardUserDefaults().setObject(relativePath, forKey: "path")
NSUserDefaults.standardUserDefaults().synchronize()
读取数据:
let possibleOldImagePath = NSUserDefaults.standardUserDefaults().objectForKey("path") as String?
if let oldImagePath = possibleOldImagePath {
let oldFullPath = self.documentsPathForFileName(oldImagePath)
let oldImageData = NSData(contentsOfFile: oldFullPath)
// here is your saved image:
let oldImage = UIImage(data: oldImageData)
}
documentsPathForFileName:
func documentsPathForFileName(name: String) -> String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);
let path = paths[0] as String;
let fullPath = path.stringByAppendingPathComponent(name)
return fullPath
}
这篇关于在NSUserDefaults中保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!