CoreData (for iphone) 存储图片 [英] CoreData (for iphone) storing images

查看:62
本文介绍了CoreData (for iphone) 存储图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将带有核心数据的图像存储到二进制属性中是否是一个明智的选择

i wonder if its a wise choice to store images with core data into binary property

假设我有一系列电影,我想将图像 dvd 封面保存到一个属性中,封面的平均大小为 20/30kb (320x480px)

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)

我想这样做的原因是为了存储管理,一旦我删除了电影,我就知道图像也被删除了

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 ?

推荐答案

在我看来,将图像存储在核心数据中并不是一个好主意,而且我很确定我也读过它Apple 的编程指南.考虑一下,当你获取你的集合时,所有的图像也会被加载到内存中,即使你只显示 8 个图像,你的整个图像集合也会被核心数据加载.

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.

如果您想确保在删除移动记录时删除图像文件,我建议您收听 managedObjectContext 的通知并在删除电影时删除文件:

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:

您可以注册 willSave 或 didSave 通知,每个都有自己的优点/缺点.

You can either register for the willSave or didSave notification, each with it's own advantages/disadvantages.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:managedObjectContext];

获取删除的对象:

- (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.
    }
}

这篇关于CoreData (for iphone) 存储图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆