RestKit图片上传

我正在使用RestKit来驱动与我的Web服务器的交互。 我正在尝试使用路由将POST事件对象添加到附加有图像的服务器。 代码如下:

RKObjectManager *manager = [RKObjectManager sharedManager]; RKObjectMapping *map = [self eventMapping]; manager.serializationMIMEType = RKMIMETypeFormURLEncoded; map.rootKeyPath = @"event"; [manager.mappingProvider setSerializationMapping:map forClass:[Event class]]; [manager.router routeClass:[Event class] toResourcePath:@"/v1/events.json" forMethod:RKRequestMethodPOST]; [manager postObject:event delegate:self block:^(RKObjectLoader *loader){ RKObjectMapping *serMap = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[Event class]]; NSError *error = nil; NSDictionary *d = [[RKObjectSerializer serializerWithObject:event mapping:serMap] serializedObject:&error]; RKParams *p = [RKParams paramsWithDictionary:d]; [p setData:[event imageData] MIMEType:@"image/jpeg" forParam:@"image"]; loader.params = p; }]; 

如果我使用序列化的Event对象创buildRKParams的实例,然后添加图像数据并将其指定为RKObjectLoader的params属性,则所有属性将成为一个大型序列化string。 必须有一种方法来上传没有大量string序列化的图像。

我也试过有一个映射到某些属性的NSData属性,沿途将UIImage转换为NSData,但是RestKit抱怨它无法映射。 有没有人做过这个?

我做了一些非常相似的事情,结果很好。 我意识到你的问题是关于为什么RKObjectSerializer没有按你期望的方式工作,但也许这是你的设置的其他东西。 我张贴我的代码给一个干净的例子,可以工作。 这就是说,在阅读了RKObjectSerializer文档之后,我不明白为什么你不能像这样在我的例子中直接设置它们,而不是像这样直接初始化你的RKParams。

路由器设置:

 RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:kApiUrlBase]; [objectManager.router routeClass:[PAPetPhoto class] toResourcePath:@"/pet/uploadPhoto" forMethod:RKRequestMethodPOST]; 

映射设置:

 RKObjectMapping *papetPhotoMapping = [RKObjectMapping mappingForClass:[PAPetPhoto class]]; [papetPhotoMapping mapKeyPath:@"id" toAttribute:@"identifier"]; [papetPhotoMapping mapAttributes:@"accountId", @"petId", @"photoId", @"filename", @"contentType", nil]; [objectManager.mappingProvider addObjectMapping:papetPhotoMapping]; [objectManager.mappingProvider setSerializationMapping:[papetPhotoMapping inverseMapping] forClass:[PAPetPhoto class]]; [objectManager.mappingProvider setMapping:papetPhotoMapping forKeyPath:@"petPhoto"]; 

post:(注意,因为我build立了所有的参数,我的对象只是一个虚拟实例来触发正确的路由和映射器)。

  PAPetPhoto *photo = [[PAPetPhoto alloc] init]; [[RKObjectManager sharedManager] postObject:photo delegate:self block:^(RKObjectLoader *loader){ RKParams* params = [RKParams params]; [params setValue:pet.accountId forParam:@"accountId"]; [params setValue:pet.identifier forParam:@"petId"]; [params setValue:_photoId forParam:@"photoId"]; [params setValue:_isThumb ? @"THUMB" : @"FULL" forParam:@"photoSize"]; [params setData:data MIMEType:@"image/png" forParam:@"image"]; loader.params = params; }]; 

服务器端点 (Java,Spring MVC)

  @RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST) @ResponseBody public Map<String, Object> handleFormUpload(@RequestParam("accountId") String accountId, @RequestParam("petId") String petId, @RequestParam("photoId") String photoId, @RequestParam("photoSize") PhotoSizeEnum photoSize, @RequestParam("image") Part image) throws IOException { if (log.isTraceEnabled()) log.trace("uploadPhoto. accountId=" + accountId + " petId=" + petId + " photoId=" + photoId + " photoSize=" + photoSize); PetPhoto petPhoto = petDao.savePetPhoto(accountId, petId, photoId, photoSize, image); Map<String, Object> map = GsonUtils.wrapWithKeypath(petPhoto, "petPhoto"); return map; } 

服务器响应JSON (注意对应于映射设置的“petPhoto”的keyPath):

 { petPhoto = { accountId = 4ebee3469ae2d8adf983c561; contentType = "image/png"; filename = "4ebee3469ae2d8adf983c561_4ec0983d036463d900841f09_3FED4959-1042-4D8B-91A8-76AA873851A3"; id = 4ee2e80203646ecd096d5201; petId = 4ec0983d036463d900841f09; photoId = "3FED4959-1042-4D8B-91A8-76AA873851A3"; }; } 

代表:

 - (void) objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object { if ([objectLoader wasSentToResourcePath:@"/pet/uploadPhoto"]) { PAPetPhoto *photo = (PAPetPhoto*)object; } }