如何将UIImages存储在我的Core Data数据库中? [英] How should I store UIImages within my Core Data database?

查看:102
本文介绍了如何将UIImages存储在我的Core Data数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它需要大约100张图片或者更多的预先插入到核心数据数据库以及其他相关信息。



现在我可以轻松添加其他数据只需写几行代码,但对于UIImages我不确定如何做,而无需编写大量的代码。我想知道:有没有反正很容易做到这一点,或者如果没有什么是最少的努力实现这个最好的方法。



此外,可以将图像存储在Core Data数据库中,还是应该只保存本地文件系统上的图像地址?

解决方案

在Core Data数据库中存储图像非常容易。您只需要将您的图像属性标记为可变换的,并创建NSValueTransformer的子类。在该子类中,添加如下代码:

  +(Class)convertedValueClass 
{
return [ NSData class];
}

+(BOOL)allowReverseTransformation
{
return YES;
}

- (id)transformedValue:(id)value
{
if(value == nil)
return nil;

//生成图像时传递原始数据,直接保存到数据库
if([value isKindOfClass:[NSData class]])
return value;

return UIImagePNGRepresentation((UIImage *)value);
}

- (id)reverseTransformedValue:(id)value
{
return [UIImage imageWithData:(NSData *)value];
}

对于您的transformable属性,请将此子类的名称指定为值变换器名称。 / p>

然后,您可以为托管此image属性的实体创建NSManagedObject子类,并声明此图像属性的属性:

  @property(nonatomic,retain)UIImage * thumbnailImage; 

您可以从UIImages读取UIImages并将其写入此属性,并且它们将透明地更改为NSData以存储在数据库中。



是否执行此操作取决于您的具体情况。较大的图像可能不应以这种方式存储,或至少应该在其自己的实体中,以便它们不被提取到存储器中,直到跟随它们之间的关系。小缩略图可能很好,以这种方式放入您的数据库。


I am developing an application which demands around 100 images or maybe more to be pre-inserted into the Core Data database along with other related information.

Now I can easily add other data by just writing a few lines of code but for UIImages I am unsure how to do it without writing a lot of code. I was wondering: is there anyway to do this easily, or if there isn't what's the best way to achieve this with the least amount of effort.

Also, is it okay to store images in a Core Data database or should we only only save the addresses of images on the local file system?

解决方案

Storing images within a Core Data database is pretty easy to do. You just need to mark your image attribute as a transformable one and create a subclass of NSValueTransformer. Within that subclass, add code like the following:

+ (Class)transformedValueClass 
{
    return [NSData class]; 
}

+ (BOOL)allowsReverseTransformation 
{
    return YES; 
}

- (id)transformedValue:(id)value 
{
    if (value == nil)
        return nil;

    // I pass in raw data when generating the image, save that directly to the database
    if ([value isKindOfClass:[NSData class]])
        return value;

    return UIImagePNGRepresentation((UIImage *)value);
}

- (id)reverseTransformedValue:(id)value
{
    return [UIImage imageWithData:(NSData *)value];
}

For your transformable attribute, specify this subclass's name as the Value Transformer Name.

You can then create an NSManagedObject subclass for the entity hosting this image attribute and declare a property for this image attribute:

@property(nonatomic, retain) UIImage *thumbnailImage;

You can read UIImages from and write UIImages to this property and they will be transparently changed to and from NSData to be stored in the database.

Whether or not to do this depends on your particular case. Larger images probably should not be stored in this manner, or at the least should be in their own entity so that they are not fetched into memory until a relationship to them is followed. Small thumbnail images are probably fine to put in your database this way.

这篇关于如何将UIImages存储在我的Core Data数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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