7

问题

我一直在尝试使用包含图像附件的多格式请求发布到服务器。我没有遇到问题将图像发送到服务器,这是其他信息未正确发送。

细节

从服务器接收对象时,我使用对象映射来配置几个不同的属性:

//Using a custom class to map object I receive to
RKObjectMapping * memoryMapper = [RKObjectMapping mappingForClass:[MemoContent class]];
[memoryMapper mapAttributes:@"created", @"user", @"participants", @"tags", @"text", @"kind", @"video", @"location", nil];
[memoryMapper mapKeyPath:@"_id" toAttribute:@"memoryID"];

//MediaMapper handles the data needed for the Image attachments
RKObjectMapping * mediaMapper = [RKObjectMapping mappingForClass:[MemoMedia class]];
[mediaMapper mapKeyPath:@"processed" toAttribute:@"processed"];
[mediaMapper mapKeyPath:@"original" toAttribute:@"original"];
[mediaMapper mapKeyPath:@"mime" toAttribute:@"mimeType"];
[memoryMapper mapKeyPath:@"media" toRelationship:@"rawMedia" withMapping:mediaMapper];

//
[[RKObjectManager sharedManager].mappingProvider setMapping:memoryMapper forKeyPath:@"memories"];
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeFormURLEncoded;
[RKObjectManager sharedManager].acceptMIMEType = RKMIMETypeJSON;

然后,当需要发布照片时,我会更新配置如下:

 RKObjectMapping * memoryMapper = [RKObjectMapping mappingForClass:[MemoContent class]];
[memoryMapper mapAttributes:@"created", @"participants",  nil];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:memoryMapper forClass:[MemoContent class]];
[[RKObjectManager sharedManager].mappingProvider setMapping:memoryMapper forKeyPath:@"memory"];

参与者是带有照片标签的人。这是我发布它的方式,类似于此https://github.com/RestKit/RestKit/wiki/Attach-a-File-to-an-RKObjectLoader

   [[RKObjectManager sharedManager] postObject:theMemory usingBlock:^(RKObjectLoader * loader){

    RKObjectMapping* serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[MemoContent class]];
    NSLog(@"serializationMapping: %@", serializationMapping);
    loader.delegate = APP; //main app delegate posting, updating
    NSError* error = nil;
    RKObjectSerializer * serializer = [[RKObjectSerializer alloc] initWithObject:theMemory mapping:serializationMapping];
    NSDictionary * dictionary = [serializer serializedObject:&error];  
    RKParams * params = [RKParams paramsWithDictionary:dictionary];
    NSData * imageData = UIImagePNGRepresentation(theMemory.photo); //image data
    [params setData:imageData MIMEType:@"image/png" forParam:@"attachment"];
    loader.params = params;
    loader.serializationMIMEType = RKMIMETypeFormURLEncoded;
    }];

服务器正在按计划接收图像,实际上确实收到了“创建”和“参与者”,不幸的是它的格式很奇怪,服务器无法理解。它包括换行符等participants (\n 19843589323 \n created: \n 3-31-2012 00:00 (类似的,当我可以访问日志时我会更新。

我会给你任何你需要的额外信息。如果我有足够的能力,我会为它提供声誉;)

4

2 回答 2

14

RestKit 0.20.0-pre3中,RKObjectManager确实有方法multipartFormRequestWithObject:method:path:parameters:constructingBodyWithBlock:

于 2012-12-12T20:18:25.747 回答
11

这个任务的一个例子可以在RestKit Github 页面找到:

Article *article = [Article new];
UIImage *image = [UIImage imageNamed:@"some_image.png"];

// Serialize the Article attributes then attach a file
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:article method:RKRequestMethodPOST path:nil parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:UIImagePNGRepresentation(image)
                                name:@"article[image]"
                            fileName:@"photo.png"
                            mimeType:@"image/png"];
}];

RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request success:nil failure:nil];
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation]; // NOTE: Must be enqueued rather than started
于 2014-05-14T15:06:09.920 回答