問題
我一直在試圖通過包含圖像附件的多種請求發佈到服務器。我沒有遇到麻煩的圖像到服務器,這是其他信息不正確發送。Restkit MultiForm Post with a Image
詳細
我使用對象映射從服務器接收對象時,配置多個不同的屬性:
//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
(類似的東西,我將在更新日誌時訪問日誌。)
我會給你任何額外的信息,如果我有足夠的信息, )
你的意思是與內容型'一個POST multipar噸/形狀data'?你堅持使用RestKit嗎? – leo
我寧願堅持使用Restkit,而且我確實相信多部分/表單數據是正確的..但是您能否詳細說明一下? –
無法幫助您使用RestKit,但如果這不是必需的,則可以使用許多其他解決方案。今天早上我在網上發佈了一個多部分/表單數據的遊戲,看看[https://gist.github.com/3786554](https://gist.github.com/3786554) – leo