简体   繁体   English

CoreData(用于iphone)存储图像

[英]CoreData (for iphone) storing images

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 ? 有人有这方面的经验吗?

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. 在我看来,将图像存储在核心数据中并不是一个好主意,我很确定我也会在Apple的一个编程指南中阅读它。 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. 考虑到这一点,当您获取集合时,所有图像也将被加载到内存中,即使您只显示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.
    }
}

最好的方法是在托管对象中存储图像的路径,然后在对象的prepareForDeletion方法中删除该文件。

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. 您可以在NSManagedObject上实现didSave或willSave方法并检查isDeleted属性。 If it is deleted, delete the image from disk. 如果删除,请从磁盘中删除该图像。

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. 您现在可以使用“允许外部存储”选项将大型文件存储在Core Data中。 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来管理文件,同时仍然可以通过路径访问REAL文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM