xcode CoreData (for iphone) 存储图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2131722/
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
CoreData (for iphone) storing images
提问by Andy Jacobs
i wonder if its a wise choice to store images with core data into binary property
我想知道将带有核心数据的图像存储到二进制属性中是否是明智的选择
say i have a collection of movies and i want to save the image dvd cover into a property the avg size of a cover is 20/30kb (320x480px)
假设我有一系列电影,我想将图像 DVD 封面保存到一个属性中,封面的平均大小为 20/30kb (320x480px)
the reason i want to do this is for storage management, once i delete the movie i know the image is also deleted
我想这样做的原因是为了存储管理,一旦我删除了电影,我知道图像也被删除了
i'm just not quite sure if it's a good idea, data load, speed?
我只是不太确定这是否是一个好主意,数据负载,速度?
anyone has experience with this ?
有人有这方面的经验吗?
回答by Ron Srebro
It seems to me that storing images in a core data isn't a good idea, and I'm pretty sure I've also read it on one of Apple's programming guides. Just consider this, when you fetch your collection, all the images will also be loaded into memory, even if you're only displaying 8 images, your entire image collection will be loaded by core data.
在我看来,将图像存储在核心数据中并不是一个好主意,而且我很确定我也在 Apple 的编程指南之一上阅读过它。试想一下,当你获取你的收藏时,所有的图片也会被加载到内存中,即使你只显示 8 张图片,你的整个图片收藏也会被核心数据加载。
If you want to make sure you delete the image files when a move record was deleted I suggest you listen to notifications by the managedObjectContext and delete files when a movie was deleted:
如果您想确保在删除移动记录时删除图像文件,我建议您收听 managedObjectContext 的通知并在删除电影时删除文件:
You can either register for the willSave or didSave notification, each with it's own advantages/disadvantages.
您可以注册 willSave 或 didSave 通知,每个通知都有自己的优点/缺点。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:managedObjectContext];
Getting the deleted objects:
获取已删除的对象:
- (void) contextDidSave:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
for (NSManagedObject *currObject in [userInfo objectForKey:NSDeletedObjectsKey])
{
// Code for deleting file associated with the NSManagedObject, you can still
// access the managed object's properties.
}
}
回答by TechZen
The best method is to store the path to the image in the managed object and then delete the file in the object's prepareForDeletion
method.
最好的方法是将图像的路径存储在托管对象中,然后在对象的prepareForDeletion
方法中删除文件。
回答by Ron Srebro
It occurred to me that you can use another approach which might be better if you're already subclassing NSManagedObject for your movie entity.
我突然想到,如果您已经为电影实体子类化 NSManagedObject,您可以使用另一种方法可能会更好。
You can implement the didSave or willSave method on the NSManagedObject and check isDeleted property. If it is deleted, delete the image from disk.
您可以在 NSManagedObject 上实现 didSave 或 willSave 方法并检查 isDeleted 属性。如果已删除,请从磁盘中删除该图像。
回答by G. Shearer
See my answer in:
见我的回答:
Can I access the files used for external binary storage in Core Data?
我可以访问 Core Data 中用于外部二进制存储的文件吗?
You can store large files in Core Data now using the 'Allows External Storage' option. The only problem with this is no longer having access to the actual files by path anymore, only the pure binary data. I came up with a 'creative' solution (whether it best practice is anyones guess), but it will allow you to use Core Data to manage your files while still being able to access the REAL files by their path.
您现在可以使用“允许外部存储”选项将大文件存储在 Core Data 中。唯一的问题是不再可以通过路径访问实际文件,只能访问纯二进制数据。我想出了一个“创造性”的解决方案(无论是最佳实践是否有人猜测),但它允许您使用 Core Data 来管理您的文件,同时仍然能够通过它们的路径访问真实文件。