RestKit .20.x值转换 [英] RestKit .20.x value transformation

查看:212
本文介绍了RestKit .20.x值转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的iOS开发,我想在RestKit做一些自定义值转换,但我不知道如何做。我有两个当前的问题:

I am new to iOS development and am trying to do some custom value transformation in RestKit, but I can't figure out how to do it. I have two current problems:


  1. 我有一个base64编码的图像,在服务器的JSON消息中返回。它是以下格式。我使用Core Data作为后端持久存储,'image'字段作为二进制数据(NSData)存储在Core Data中。当RestKit将base64编码的图像转换为NSData对象时,出现错误,因为存储在sqllite数据库中的图像字节不正确,我无法重新显示图像。

  1. I have a base64 encoded image that is returning in a JSON message from the server. It is in the below format. I am using Core Data as the backend persistent store and the 'image' field is stored in Core Data as binary data (NSData). When RestKit transforms the base64 encoded image into an NSData object, something goes wrong because the image bytes that are stored in the sqllite database are not correct and I can't redisplay the image.

"images": [{
 "id": 1,
 "recordId": 1,
 "status": "C",
 "type": "MUGSHOT",
 "format": "JPEG",
 "width": 50,
 "height": 50,
 "image": "/9j/4AAQSkZJRgABAQEBLAEsAA....",
 "createBy": "blah",
 "createDt": 1395683191483,
 "captureDevice": null
}]


  • createBy日期以ms为单位,不能正确转换为NSDate对象。我知道它的预期价值在几秒钟内,我假设是导致问题的原因。

  • 经过多次Google搜索发现RKValueTransformer将允许我为不同的类设置自定义变换(在我的例子中,我想为NSDate和NSData类设置一个自定义的变换器,但这里引用的RKBlockValueTransformer类( https://github.com/RestKit/RestKit/wiki/Object-Mapping )下的我使用RestKit .20.3并使用CocoaPods安装它与此Podfile:

    After much googling, I found that RKValueTransformer will allow me to set up custom transformations for different classes (in my case, I'd want to set up a custom transformer for NSDate and NSData classes. However, the RKBlockValueTransformer class that is referenced here (https://github.com/RestKit/RestKit/wiki/Object-Mapping) under the "Customizing Value Transformation" section doesn't appear to exist for me. I am using RestKit .20.3 and used CocoaPods to install it with this Podfile:

        platform :ios, '7.0'
        pod 'RestKit', '~> 0.20.3'
    

    我尝试添加pod'RKValueTransformers','〜> 1.0.0'到我的Podfile,但是引起了一些编译器问题,另外,不是 RKValueTransformers项目只是已经内置到RestKit中的值转换特性的一个提取?

    I tried adding pod 'RKValueTransformers', '~> 1.0.0' to my Podfile, but that caused some compiler issues. Plus, isn't the RKValueTransformers project just an extract of the value transformation feature already built into RestKit?

    正确的路径使用RKValueTransformer获得NSDate和NSData行为我在寻找?如果是,我该怎么做才能把它包含在我的项目中?有没有更简单的方法让我的应用程序正确处理base64编码的图像存储在Core Data作为二进制字段?

    Am I on the right path with using the RKValueTransformer to get the NSDate and NSData behavior I am looking for? If so, what do I have to do to get it included in my project? Are there any easier ways to get my application to correctly handle base64 encoded images stored in Core Data as a binary field?

    编辑:这里是我的RKValueTransformers.h和.m文件在我的Pods项目。它似乎只是日期格式化程序相关:

    Here is my RKValueTransformers.h and .m file in my Pods project. It appears to be only date formatter related:

    推荐答案

    如果您将图像数据存储在Core Data中,您应该考虑勾选允许外部存储选项。

    If you're storing image data in Core Data you should think about ticking the 'allow external storage' option.

    值变换器是将 NSString 转换为 NSData ,您需要在中间的base64。您需要创建一个符合< RKValueTransforming> 。因为你从 NSString 转换,你应该实现 validateTransformationFromClass:toClass:,所以你可以拒绝任何转换,

    The value transformer you want is to convert NSString to NSData and you need to base64 in the middle. You need to create a class which conforms to < RKValueTransforming >. Because you are transforming from NSString you should implement validateTransformationFromClass:toClass: so you can reject any transformation where the destination isn't NSData efficiently.

    一旦你创建了你的类和它的实例(安装它:

    Once you have created your class and in instance of it (imageFormatter), install it with:

    [[RKValueTransformer defaultValueTransformer] insertValueTransformer:imageFormatter atIndex:0];
    

    ,相同的过程适用于日期格式化程序。

    and the same process applies for the date formatter.

    这篇关于RestKit .20.x值转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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