ios - 使用 JsonModel 存档 UIImage

标签 ios uiimage nscoding nskeyedarchiver jsonmodel

情况是这样的: 当我从 API 获取数据时,我使用 JsonModel 将 json 转换为模型,这非常好。

而且我要对一些数据做持久化存储,最后我选择了NSKeyedArchive和NSKeyedUnarchive来保存和获取数据。 重点来了,JsonModel已经符合了NSCoding,所以我不用再写代码去应用NSCoding了。我归档和取消归档一些基本数据,效果很好。

但是数据模型中有一个 UIImage 属性,当我存档数据模型时,问题来了,存档没有完成。 (好像JsonModel不支持UIImage编码???)

当它抛出时,代码来到

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

所以任何人都知道我应该怎么做才能解决这个问题?

提前致谢!

最佳答案

你是对的 - JSONModel 不支持 UIImage 实例的(反)序列化。您应该实现自定义数据转换器 (JSONValueTransformer),as shown in the readme :

@implementation JSONValueTransformer (CustomTransformer)

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

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

@end

关于ios - 使用 JsonModel 存档 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33391216/

相关文章:

ios - 使用字典作为模型

ios - 无需下载的远程图像大小

iphone - 插入 UIImageView 作为 subview 滞后

ios - 如何以编程方式添加可重用的自定义 View ? ( swift )

swift - 使用 NSCoding 将自定义 Swift 类保存到 UserDefaults

ios - 创建一个全局变量来存储 NSDate 的时间?

iphone - 获取 UITableViewCell 文本的正确方法

ios - 如何为 iPhone/iPad 创建纵向 iAd 横幅 View

ios - Swift(iOS) 将 UIImage 数组插入 subview ?

swift - decodeObject(forKey :) & decodeObjectForKey(_:)) 之间有什么区别