Restkit MultiForm发布图像

时间:2012-09-26 15:01:59

标签: iphone objective-c ios restkit

问题

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

详细

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

//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(类似的东西,当我有权访问日志时,我会更新。

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

2 个答案:

答案 0 :(得分:14)

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

答案 1 :(得分:11)

可以在RestKit Github page

找到此任务的示例
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