使用RKObjectMapping发送图像时,RestKit映射错误

时间:2012-10-26 15:47:53

标签: iphone objective-c ios ipad restkit

这是对象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: ''

方法3

调用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大师可以帮助我解决这三种方法中的任何一种,以帮助我将一个有一个图像的对象发送到服务器。

0 个答案:

没有答案