简体   繁体   English

使用JsonModel存档UIImage

[英]archive UIImage with JsonModel

the situation is here: I use JsonModel to convert json to model when I get data from API, it's pretty good. 情况就在这里:当我从API获取数据时,我使用JsonModel将json转换为模型,这非常好。

and I have to do persistent storage for some data, I finally choose NSKeyedArchive and NSKeyedUnarchive to save and fetch data. 并且我必须对某些数据进行持久存储,最后我选择了NSKeyedArchive和NSKeyedUnarchive来保存和获取数据。 now comes the point, the JsonModel has already conform to NSCoding, so I don't need to write the code to apply the NSCoding. 现在到了要点,JsonModel已经符合NSCoding,所以我不需要编写代码即可应用NSCoding。 I archive and unarchive some basic data, it works well. 我存档并取消存档一些基本数据,效果很好。

but there is a UIImage property in a data model ,when I archive the data model, the problem comes, the archive does not complete. 但是数据模型中有一个UIImage属性,当我归档数据模型时,问题来了,归档没有完成。 (it seems the JsonModel does not support UIImage coding???) (似乎JsonModel不支持UIImage编码?)

when it throw out ,the code comes to 当它扔出去的时候,代码就到了

@throw [NSException exceptionWithName:@"Value transformer not found"
                                                   reason:[NSString stringWithFormat:@"[JSONValueTransformer %@] not found", selectorName]
                                                 userInfo:nil];

so any body knows what should i do to solve the problem? 所以任何人都知道我该怎么做才能解决问题?

thanks ahead! 谢谢你!

You're correct - JSONModel does not support (de)serialization of UIImage instances. 您是正确的-JSONModel不支持UIImage实例的(反序列化)。 You should implement a custom data transformer ( JSONValueTransformer ), as shown in the readme : 您应该实现自定义数据转换器( JSONValueTransformer ), 如自述文件所示

@implementation JSONValueTransformer (CustomTransformer)

- (UIImage *)UIImageFromNSString:(NSString *)string {
    return nil; // transformed object
}

- (NSString *)JSONObjectFromUIImage:(UIImage *)image {
    return nil; // transformed object
}

@end

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

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