1

这是对象 RestKit 映射:-

    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ImageModel class]];

[mapping mapKeyPath:@"Description" toAttribute:@"description"];
[mapping mapKeyPath:@"Photo1"  toAttribute:@"photo1"];

[objectManager.mappingProvider addObjectMapping:mapping];

RKObjectMapping* serializeMapping = [mapping inverseMapping];
[objectManager.mappingProvider setSerializationMapping:serializeMapping forClass:[ImageModel class]];
[objectManager.router routeClass:[ImageModel class] toResourcePath:@"/image" forMethod:RKRequestMethodPOST];

[objectManager.mappingProvider setObjectMapping:mapping forResourcePathPattern:@"/image"];

这是 ImageModel 对象:-

@property  (strong,nonatomic) NSString *description; 
@property  (strong,nonatomic) UIImage *photo1;

以下是我尝试过的三种不同方法,但都有错误:-

方法1。

只需使用以下代码发布对象。

obj.description = self.descriptionText.text;
obj.photo1 = selectedImage;
[[RKObjectManager sharedManager] postObject:obj delegate:self];

结果:- 对象被发布到服务器,服务器脚本返回相同的对象。客户端收到响应,但发生以下映射相关异常。

2012-10-26 20:16:39.015 APP[4548:7407] W restkit.object_mapping:RKObjectMappingOperation.m:244 Failed transformation of value at keyPath 'Photo1'. No strategy for transforming from '__NSCFString' to 'UIImage'

日志确实显示返回对象的 Photo1 字段确实有照片数据,但是映射中遗漏的内容是无法理解的。RestKit 大师的任何想法都会非常有帮助!

方法 2 - 调用 sendObject:toResourcePath:usingBlock

[[RKObjectManager sharedManager] sendObject:self.obj toResourcePath:@"/image" usingBlock:^(RKObjectLoader *loader) {
    loader.targetObject = nil;
    loader.delegate = self;
    loader.method = RKRequestMethodPOST;

    if([obj photo1]){
        RKObjectMapping* serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[ImageModel class]];
        NSError* error = nil;
        NSDictionary* dictionary = [[RKObjectSerializer serializerWithObject:obj mapping:serializationMapping] serializedObject:&error];
        NSLog(@"%@", dictionary);
        RKParams* params = [RKParams paramsWithDictionary:dictionary];
        NSData* imageData =  UIImagePNGRepresentation([obj photo1]);
        [params setData:imageData MIMEType:@"image/png" forParam:@"Photo1"];
        loader.params = params;
    }
}];

结果:日志窗口中出现以下错误。

2012-10-26 20:32:15.327 APP[4627:c07] response code: 500
2012-10-26 20:32:22.388 APP[4627:c07] Loaded payload: {"Message":"An error has occurred."}
2012-10-26 20:32:27.878 APP[4627:c07] W restkit.object_mapping:RKObjectMapper.m:87 Adding mapping error: Could not find an object mapping for keyPath: ''
2012-10-26 20:32:27.878 APP[4627:c07] E restkit.network:RKObjectLoader.m:231 Encountered errors during mapping: Could not find an object mapping for keyPath: ''

方法三

调用 postObject:usingBlock

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

    loader.delegate = self;

    RKParams* params = [RKParams params];
    [params setValue:obj.description forParam:@"Description"];
    [params setData:[app convertImageToNSData: [app photo1]] MIMEType:@"image/png" forParam:@"Photo1"];        
    loader.params = params;
}];

结果:- 日志窗口中出现以下异常。

2012-10-26 20:42:07.735 APP[4670:c07] response code: 500
2012-10-26 20:42:10.726 APP[4670:c07] Loaded payload: {"Message":"An error has occurred."}
2012-10-26 20:42:16.569 APP[4670:c07] W restkit.object_mapping:RKObjectMapper.m:87 Adding mapping error: Could not find an object mapping for keyPath: ''
2012-10-26 20:42:16.570 APP[4670:c07] E restkit.network:RKObjectLoader.m:231 Encountered errors during mapping: Could not find an object mapping for keyPath: ''
2012-10-26 20:42:16.570 APP[4670:c07] E restkit.network:RKObjectLoader.m:360 Encountered an error while attempting to map server side errors from payload: Could not find an object mapping for keyPath: ''

显然我在映射中没有遗漏任何东西,但我可能是 . RestKit 专家可以帮助我使用这 3 种方法中的任何一种,以帮助我将在其中一个字段中具有图像的对象发送到服务器。

4

0 回答 0