问题描述
我想将NSData转换为字节数组,下面给出的是我使用的代码
I want to convert the NSData into byte array and given below is the code that i have used
NSData *imageData = UIImagePNGRepresentation(recipeImage.image);
NSUInteger len = [imageData length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [imageData bytes], len);
NSLog(@"%8s",byteData);
但是当我将byteData发布到下面给出的Web服务时,这给了我一个错误
But its giving me an error when i post the byteData to the webservice which is given below
服务器无法处理请求.--->参数无效."
"Server was unable to process request. ---> Parameter is not valid."
当我打印byteData时,这就是我在控制台中得到的
and when i print the byteData this is what i get in the console
âPNG
我尝试在文档中搜索NSData并找到了getBytes方法,但这也没有任何用处,我仍然遇到相同的错误.
I tried searching the docs for NSData and found the getBytes method but that too was not of any use i was still getting the same error.
能否让我从上面的代码中得知我在哪里或将数据转换为字节数组时犯了什么错误
Could you please let me know from the code above as in where i am wrong or what mistake i am making in converting the data into byte array
我尝试使用
[imageData getBytes:&byteData length:length];
这给我带来了严重的访问错误
Its giving me a bad access error
推荐答案
尝试一下
NSData *imageData = UIImagePNGRepresentation(recipeImage.image);
NSUInteger len = [imageData length];
Byte *byteData= (Byte*)malloc(len);
[imageData getBytes:byteData length:len];
这篇关于将NSData转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!