Iphone 将NSData转换为字节数组

Iphone 将NSData转换为字节数组,iphone,objective-c,ios,xcode,nsdata,Iphone,Objective C,Ios,Xcode,Nsdata,我想把NSData转换成字节数组,下面是我使用的代码 NSData *imageData = UIImagePNGRepresentation(recipeImage.image); NSUInteger len = [imageData length]; Byte *byteData = (Byte*)malloc(len); memcpy(byteData, [imageData bytes], len); NSLog(@"%8s",byteData); NS

我想把NSData转换成字节数组,下面是我使用的代码

NSData *imageData = UIImagePNGRepresentation(recipeImage.image); NSUInteger len = [imageData length]; Byte *byteData = (Byte*)malloc(len); memcpy(byteData, [imageData bytes], len); NSLog(@"%8s",byteData); NSData*imageData=UIImagePNGRepresentation(recipeImage.image); NSU整数长度=[imageData长度]; 字节*字节数据=(字节*)malloc(len); memcpy(字节数据,[imageData字节],len); NSLog(@“%8s”,字节数据); 但是当我将byteData发布到下面给出的Web服务时,它给了我一个错误

“服务器无法处理请求。-->参数无效。”

当我打印字节数据时,这就是我在控制台中得到的

âPNG 巴布亚新几内亚 我尝试在文档中搜索NSData并找到getBytes方法,但这也没有任何用处,我仍然收到相同的错误

你能让我从上面的代码中知道我错在哪里,或者我在将数据转换成字节数组时犯了什么错误吗

编辑:我试过使用

[imageData getBytes:&byteData length:length]; [imageData getBytes:&字节数据长度:长度]; 它给了我一个错误的访问

NSData *imageData = UIImagePNGRepresentation(recipeImage.image);  
NSUInteger len = [imageData length];
Byte *byteData= (Byte*)malloc(len);
[imageData  getBytes:byteData length:len];
试试这个

NSData *imageData = UIImagePNGRepresentation(recipeImage.image);  
NSUInteger len = [imageData length];
Byte *byteData= (Byte*)malloc(len);
[imageData  getBytes:byteData length:len];

你的问题完全不同


您正在向web服务发送数据,但失败了向我们展示如何将数据发送到web服务。将NSData转换为字节数组是毫无意义的。字节是一个非常好的字节数组

你的问题完全不同


您正在向web服务发送数据,但失败了向我们展示如何将数据发送到web服务。将NSData转换为字节数组是毫无意义的。字节是一个非常好的字节数组

你想干什么?从图片中获取像素数据?删除
&
,错误访问错误将消失。无论如何,我看不出这段代码的意义-为什么不将数据留在
NSData
对象中,避免额外的内存和复制数据的开销?您也可以使用
byte*byteData=imageData.bytes直接引用字节数组你想做什么?从图片中获取像素数据?删除
&
,错误访问错误将消失。无论如何,我看不出这段代码的意义-为什么不将数据留在
NSData
对象中,避免额外的内存和复制数据的开销?您也可以使用
byte*byteData=imageData.bytes直接引用字节数组
@parabafna我必须向webservice(imageData/len/byteData)发送什么参数?不用说,如果您使用这个
malloc
模式,请确保在使用
byteData
时,您所有人都
free
,否则您将泄漏。@parabafna我必须向webservice(imageData/len/byteData)发送什么参数?不用说,如果您使用此
malloc
模式,请确保在使用
byteData
时,所有
free
,否则会泄漏。