Convert NSData to byte array

风格不统一 提交于 2019-12-13 17:50:01

问题


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);

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."

and when i print the byteData this is what i get in the console

âPNG

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

Edit: I have tried using the

[imageData getBytes:&byteData length:length];

Its giving me a bad access error


回答1:


Try this

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



回答2:


Your problem is somewhere totally different.

You are sending data to a web service, and that fails. Show us how you send the data to the web service. Converting NSData to a byte array is absolutely pointless. imageData.bytes is as good a byte array as you are ever going to get.



来源:https://stackoverflow.com/questions/10696391/convert-nsdata-to-byte-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!