在将byte 转换为NSData之后,数据会损坏

我有.Net Web服务响应包含一个字节[]条目等字段。 数据是一个PDF文件。

我从接收到的数据中提取字典:[NSJSONSerialization JSONObjectWithData]

此后我使用下面的代码将字节[]转换为NSData。 然后将结果保存到磁盘(请参见最后一行)。

打开生成的PDF文件时,出现以下错误:

“未能findPDF头:`%PDF'找不到。”

NSArray *byteArray = [rootDictionary objectForKey:@"file"]; unsigned c = byteArray.count; uint8_t *bytes = malloc(sizeof(*bytes) * c); unsigned i; for (i = 0; i < c; i++) { NSString *str = [byteArray objectAtIndex:i]; int byte = [str intValue]; bytes[i] = (uint8_t)byte; } NSData* data = [NSData dataWithBytes:(const void *)byteArray length:sizeof(unsigned char)*c]; //Save to disk using svc class. NSString *localPath = [svc saveReport:data ToFile:[rootDictionary objectForKey:@"name"]]; 

我也尝试将byte []转换为base64 NSString(在服务端),然后返回到我的应用程序,其中工作(主要是**)的NSData,但我被告知,这是sl code的代码。

**当同时拉多个PDFasynchronous时,这些以base64stringforms接收的报告也被破坏。

PS。 请让我知道,如果我还必须提供我的svc类的代码,但我不认为问题在那里。

编辑:我创build了一个新的Web服务方法,需要一个字节[]作为input,然后修改我的iOS应用程序发送byteArrayvariables回到服务,它被保存到一个文件。 生成的PDF文件是Adobe可读的有效文件。 意思是在转移过程中没有腐败。

谢谢!

好的,最后把这些东西整理出来,然后仔细梳理我的代码(由http://www.raywenderlich.com/forums/viewtopic.php?f=2&p=38590#p38590的snadeep.gvn启发)&#x3002;

我犯了一个愚蠢的错误,我忽略了100多次。

这行代码:

 NSData* data = [NSData dataWithBytes:(const void *)byteArray length:sizeof(unsigned char)*c]; 

应改为:

 NSData* data = [NSData dataWithBytes:(const void *)bytes length:sizeof(unsigned char)*c]; 

好时光,现在我终于可以睡一觉了:-)