1
 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 有问题吗?我不知道这个问题。任何帮助将不胜感激。在此先感谢。

4

2 回答 2

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),请参阅

于 2013-09-06T07:53:21.917 回答
1

您不能发送NSDataas JSON,如果要发送图像,JSON则必须先对其进行编码,Base64然后在服务器上对其进行解码

看看这个 https://github.com/l4u/NSData-Base64

于 2013-09-06T07:02:26.080 回答