在 iOS 设备上本地存储图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14531912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing images locally on an iOS device
提问by SundayMonday
Some iOS photo-related apps store images created with the app somewhere other than the photo library. For example Fat Booth shows a scrolling list of photos created with the app at app start-up. Note these photos are persisted w/o the user explicitly saving them to the photo library. What is the simplest way to save and persist images inside an iOS application?
一些与 iOS 照片相关的应用程序会将使用该应用程序创建的图像存储在照片库以外的其他地方。例如,Fat Booth 显示在应用程序启动时使用该应用程序创建的照片的滚动列表。请注意,这些照片不会被用户明确保存到照片库中。在 iOS 应用程序中保存和保留图像的最简单方法是什么?
The only persistent stores I'm familiar with are NSUserDefaults and the key chain. However I've never heard of these used to store larger pieces of data such as an image. Now I'm wondering if Core Data is the easiest way.
我熟悉的唯一持久存储是 NSUserDefaults 和钥匙链。但是,我从未听说过这些用于存储较大的数据(例如图像)。现在我想知道 Core Data 是否是最简单的方法。
回答by Tommy Devoy
The simplest way is to save it in the app's Documents directory and save the path with NSUserDefaults like so:
最简单的方法是将其保存在应用程序的 Documents 目录中,并使用 NSUserDefaults 保存路径,如下所示:
NSData *imageData = UIImagePNGRepresentation(newImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"cached"]];
NSLog(@"pre writing to file");
if (![imageData writeToFile:imagePath atomically:NO])
{
NSLog(@"Failed to cache image data to disk");
}
else
{
NSLog(@"the cachedImagedPath is %@",imagePath);
}
Then save the imagePath in some dictionary in NSUserDefaults or however you'd like, and then to retrieve it just do:
然后将 imagePath 保存在 NSUserDefaults 中的某个字典中,或者您愿意,然后检索它只需执行以下操作:
NSString *theImagePath = [yourDictionary objectForKey:@"cachedImagePath"];
UIImage *customImage = [UIImage imageWithContentsOfFile:theImagePath];
回答by Segev
For Swift:
对于斯威夫特:
let imageData = UIImagePNGRepresentation(selectedImage)
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let imagePath = paths.stringByAppendingPathComponent("cached.png")
if !imageData.writeToFile(imagePath, atomically: false)
{
println("not saved")
} else {
println("saved")
NSUserDefaults.standardUserDefaults().setObject(imagePath, forKey: "imagePath")
}
For Swift 2.1:
对于 Swift 2.1:
let imageData = UIImagePNGRepresentation(selectedImage)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let imageURL = documentsURL.URLByAppendingPathComponent("cached.png")
if !imageData.writeToURL(imageURL, atomically: false)
{
print("not saved")
} else {
print("saved")
NSUserDefaults.standardUserDefaults().setObject(imageData, forKey: "imagePath")
}
stringByAppendingPathComponent
is unavailable in Swift 2.1 so you can use URLByAppendingPathComponent.
Get more info here.
stringByAppendingPathComponent
在 Swift 2.1 中不可用,因此您可以在此处使用URLByAppendingPathComponent.
获取更多信息。
回答by Sean
You cando it with core data by storing binary data, but its not recommended. There is a much better way - especially for photos. Your application has a documents/file directory that only your app can access. I recommend starting here for the concepts and how to access it. Its relatively straightforward. You may want to combine this with core data to store file paths, metadata, etc. http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html
您可以通过存储二进制数据来处理核心数据,但不建议这样做。有一个更好的方法 - 特别是对于照片。您的应用程序有一个只有您的应用程序可以访问的文档/文件目录。我建议从这里开始了解概念以及如何访问它。它相对简单。您可能希望将其与核心数据结合起来存储文件路径、元数据等。http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUIde/FileSystemOverview/FileSystemOverview.html