objective-c - 在 RestKit 中序列化嵌套图像(Rails 后端)

标签 objective-c ruby-on-rails ios multipart restkit

这是我得到的:

    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 后端。在大多数情况下,它是有效的,但我似乎无法让我的多部分图像序列化。

图像属于“Review”类,而该类又嵌套在“Dish”类中。这是我到目前为止所拥有的:

// 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 属性并将其发送出去:

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

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

正确的做法是什么?

最佳答案

您是否尝试手动将嵌套添加到参数名称中:

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

关于objective-c - 在 RestKit 中序列化嵌套图像(Rails 后端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203182/

相关文章:

iOS:将字符串数据转换为 2 字节数组并发送到 CRC

ruby-on-rails - 如何在使用 thin 时增加 Faye 中的最大连接数

ios - tableView.cellForRow(at :) and tableView. dataSource?tableView(tableView:cellForRowAt:) 之间的区别

iphone - 如何比较 Objective C 中的日期

ios - 使用自动布局将模态视图居中

ruby-on-rails - Rails 如何处理多个传入请求?

security - 在 iOS 上,与使用 CFStream、CFNetwork 等安全套接字相比,OpenSSL 有什么好处?

ios - 添加 Apple Watch 扩展后,Xcode 卡在加载轮上

ios - 我需要4条If语句,然后如果所有条件都正确,则需要1条else语句

ruby-on-rails - 如何使用新方法扩展 ActiveRecord 关系?