objective-c – RestKit图像上传

objective-c – RestKit图像上传,第1张

概述我正在使用RestKit来驱动与我的Web服务器的互动.我正在尝试使用路由将一个Event对象POST到一个连接到它的服务器上.代码如下: RKObjectManager *manager = [RKObjectManager sharedManager]; RKObjectMapping *map = [self eventMapping]; manager.serialization 我正在使用RestKit来驱动与我的Web服务器的互动.我正在尝试使用路由将一个Event对象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;  }];

如果我使用序列化的事件对象创建一个RKParams的实例,则添加图像数据并将其分配为RKObjectLoader的params属性,所有属性都将成为一个巨大的序列化字符串.必须有一种方法来上传图像,而不需要大量字符串序列化.

我还尝试将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"];

(我注意到,自从我建立了所有我的params在块中我的对象只是一个虚拟实例来触发正确的路由和映射器).

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;    }}
总结

以上是内存溢出为你收集整理的objective-c – RestKit图像上传全部内容,希望文章能够帮你解决objective-c – RestKit图像上传所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1245957.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存