在RestKit中序列化嵌套图像(Rails后端) [英] Serialize nested image in RestKit (Rails Backend)

查看:70
本文介绍了在RestKit中序列化嵌套图像(Rails后端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我要得到的:

    food_name_token Pizza
    id  1
    category_id 1
    review  {"note"=>"tasty!", "image"=>"<RKParamsAttachment: 0x7834500>"}

这就是我所期望的:

    food_name_token Pizza
    id  1
    category_id 1
    review  {"note"=>"tasty!", "image"=>{"filename"=>"image", "type"=>"image/png", "name"=>"image", "tempfile"=>"#", "head"=>"Content-Disposition: form-data; name=\"image\"; filename=\"image\"\r\nContent-Type: image/png\r\n"}}

我正在使用Restkit,并且试图在Iphone上序列化我的数据并将其发送到我的Rails后端.在大多数情况下,它都可以工作,但是我似乎无法让我的多部分图像进行序列化.

I'm using Restkit and I'm trying to serialize my data on the Iphone and send it to my Rails backend. For the most part, it works, but I just can't seem to get my multipart image to serialize.

该图像属于评论"类,该类又嵌套在菜"类中.这是我到目前为止的内容:

The image belongs to a 'Review' class, which in turn is nested in a 'Dish' class. Here is what I have so far:

// Set up routes
    RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://postbin.org"];
    [manager.router routeClass:[Dish class] toResourcePath:@"/5a850fe1" forMethod:RKRequestMethodPOST];  
    [manager.router routeClass:[Review class] toResourcePath:@"/5a850fe1" forMethod:RKRequestMethodPOST]; 

// Map Dish Object
    RKObjectMapping *dishMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
    [dishMapping mapKeyPath:@"id" toAttribute:@"identifier"];
    [dishMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"];
    [dishMapping mapKeyPath:@"food_name_token" toAttribute:@"name"];

// Map Review Object
    RKObjectMapping *reviewMapping = [RKObjectMapping mappingForClass:[Review class]];
    [reviewMapping mapKeyPath:@"note" toAttribute:@"note"];
    [reviewMapping mapKeyPath:@"image" toAttribute:@"image"];

// Define the relationship mapping
    [dishMapping mapKeyPath:@"review" toRelationship:@"review" withMapping:reviewMapping];

// Set serialization for Dish Class
    RKObjectMapping* dishSerializationMapping = [dishMapping inverseMapping];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:dishSerializationMapping forClass:[Dish class] ]; 

// Set serialization for Review Class 
    RKObjectMapping* reviewSerializationMapping = [reviewMapping inverseMapping];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:reviewSerializationMapping forClass:[Review class] ]; 

然后我分配值:

    [Dish sharedDish].name = @"Pizza";
    [Dish sharedDish].identifier = [NSNumber numberWithInt:1]; 
    [Dish sharedDish].categoryID = [NSNumber numberWithInt:1];

    [Review sharedReview].note = @"tasty!";

// Turn an image into an RKParamsAttachment object and assign it to the image attribute of the Review class
    RKParams* params = [RKParams params];
    UIImage* imageOrig = [UIImage imageNamed:@"pizza.jpg"];
    NSData* imageData = UIImagePNGRepresentation(imageOrig);
    RKParamsAttachment* image = [params setData:imageData MIMEType:@"image/png" forParam:@"image"];
    [params release];

    [Review sharedReview].image = image;

然后将我的Review对象分配到Dish对象的review属性并将其发送出去:

And then I assign my Review object to my Dish object's review attribute and send it off:

    [Dish sharedDish].review = [Review sharedReview];  
    [[RKObjectManager sharedManager] postObject:[Dish sharedDish] delegate:self];

似乎我以错误的方式将图像附加到对象上.

It seems like I'm attaching the image to the object the wrong way.

什么是正确的方法?

推荐答案

您尝试手动将嵌套添加到参数名称中吗?

Did you try manually adding the nesting to the param name:

RKParamsAttachment* image = [params setData:imageData 
    MIMEType:@"image/png" forParam:@"review[image]"];

这篇关于在RestKit中序列化嵌套图像(Rails后端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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