RestKit .20.x value transformation

2019-07-19 14:34发布

问题:

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. 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
    }]
    
  2. The createBy date is in ms since epoch, which isn't being transformed correctly into a NSDate object. I understand that it is expecting the value to be in seconds, which I assume what is causing the problem.

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'

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?

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?

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

回答1:

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

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.