把字节值放入NSDictionary

我有一个后期的Web服务,我需要在字节数组中发送图像,因为我需要以JSON格式发送发布参数,所以我创build了它的NSDictionary 。 我的问题是我如何分配对象ByteByte键,我试图这样做,应用程序崩溃时创buildNSDictionary 。 我也在解释每个步骤的代码,以便轻松理解下面的y问题:

这是我的代码转换图像为字节格式: –

 NSData *data = [NSData dataWithContentsOfFile:filePath]; NSUInteger len = [data bytes]; Byte *byteData = (Byte*)malloc(len); memcpy(byteData, [data bytes], len); 

所以,现在我有字节数组中包含图像的字节,现在我想将此对象分配给NSDictionary 。 这里是代码,

 NSDictionary *dictJson = [NSDictionary dictionaryWithObjectsAndKeys: byteData, @"photo", nil]; 

现在我的应用程序崩溃在上面创builddictJson对象的行,有什么办法可以传递字节到NSDictionary

另外还有一个问题,我怎么能NSLog byteData

请帮帮我。

提前致谢!

如果你真的想把图像数据作为一个包含字节的JSON数组作为数字发送,那么你必须手动创build数组,而不是内置函数:

 NSData *data = ... // your image data const unsigned char *bytes = [data bytes]; // no need to copy the data NSUInteger length = [data length]; NSMutableArray *byteArray = [NSMutableArray array]; for (NSUInteger i = 0; i < length; i++) { [byteArray addObject:[NSNumber numberWithUnsignedChar:bytes[i]]]; } NSDictionary *dictJson = [NSDictionary dictionaryWithObjectsAndKeys: byteArray, @"photo", nil]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictJson options:0 error:NULL]; 

JSON数据会看起来像

 {"photo":[byte0, byte1, byte2, ...]} 

所以也许这就是服务器所期望的。 但请注意,这是一个非常无效的(在空间方面)发送图像数据的方式(比如Base64)。

如果您将此信息作为JSON发送到服务器,则需要使用诸如Base 64之类的编码将其首先转换为有效的string。

我相信你放置在NSDictionary中的任何对象都必须从NSObject派生。

很明显Byte *不是从NSObject派生的。

也许尝试使用一个集合类,如NSArray ,或者确实尝试把NSData直接放在那里。

您可以尝试将字节放入NSValue中:

  UIImage* testImage = [UIImage imageNamed:@"Default.png"]; NSData* data = UIImagePNGRepresentation(testImage); NSValue* value = [NSValue valueWithBytes:[data bytes] objCType:@encode(UIImage)]; NSDictionary *dictJson = [NSDictionary dictionaryWithObjectsAndKeys: value, @"photo", nil]; 

使用File Object创build你的请求字典,稍后postWith:函数将操纵你的图像绑定任务

 NSDictionary *requestDict = [NSDictionary dictionaryWithObjectsAndKeys:@"FirstObjectValue",@"FirstKey", @"Second Object",@"Second Key", @"myFileParameterToReadOnServerSide",@"file", nil]; // This line indicate ,POST data has file to attach [self postWith:requestDict]; 

下面的函数将读取你想要发送的对象的字典中的所有参数,如果你想发送图像,然后在你的字典中添加“文件”键,标识有一些文件要发送请求

 - (void)postWith:(NSDictionary *)post_vars { NSString *urlString = [NSString stringWithFormat:@"YourHostString"]; NSURL *url = [NSURL URLWithString:urlString]; NSString *boundary = @"----1010101010"; // define content type and add Body Boundry NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; NSEnumerator *enumerator = [post_vars keyEnumerator]; NSString *key; NSString *value; NSString *content_disposition; while ((key = (NSString *)[enumerator nextObject])) { if ([key isEqualToString:@"file"]) { value = (NSString *)[post_vars objectForKey:key]; // *** Write Your Image Name Here *** // Covnert image to Data and bind to your post request NSData *postData = UIImageJPEGRepresentation([UIImage imageNamed:@"yourImage"], 1.0); [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\nfilename=\"testUploadFile.jpg\"\r\n\r\n",value] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:postData]; } else { value = (NSString *)[post_vars objectForKey:key]; content_disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key]; [body appendData:[content_disposition dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]]; } [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; } //Close the request body with Boundry [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; [request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(@"%@", returnString); }