Code:
NSData *imgd=UIImageJPEGRepresentation(Obj.thumbImage, 0.5);
[imgName insertObject:Obj.imageName atIndex:i];
[imgName1 insertObject:imgd atIndex:i];
[dic setObject:imgName forKey:@"name"];
[dic setObject:imgName1 forKey:@"image"];
[asiLoadingFormRequest setPostValue:contactName forKey:@"receivername"];
[asiLoadingFormRequest setPostValue:[dic JSONRepresentation] forKey:@"imagedata"];
通过json发送图像的方式是否正确?当我调用服务器时,我收到以下错误消息
"Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"Unsupported value for key image in object\" UserInfo=0x9a611f0 {NSUnderlyingError=0x9a4a290 \"JSON serialisation not supported for NSConcreteMutableData\", NSLocalizedDescription=Unsupported value for key image in object
Json是否存在不支持图像的问题?我对此问题一无所知。任何帮助都会受到赞赏。谢谢提前。
答案 0 :(得分:3)
试试这个:
编码部分:
首先将Base64课程添加到您的项目中。
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:xxx.me.com/me.json"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];
NSData *imageData = UIImagePNGRepresentation ([UIImage imageWithContentsOfFile: @"ur PNG image path"]);
[Base64 initialize];
NSString *imageString = [Base64 encode:imageData];
NSArray *keys = [NSArray arrayWithObjects:@"image_id",@"image_name","image",nil];
NSArray *objects = [NSArray arrayWithObjects:@"22",@"myImageName.png",imageString,nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:kNilOptions error:&error];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d",[jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
对于服务器端的解码(如果使用的是php),请参阅this。
答案 1 :(得分:1)
您无法将NSData
发送为JSON
,如果您要通过JSON
发送图片,则必须先在Base64
中对其进行编码,然后在服务器上对其进行解码