I have a post web service, where I need to send image in byte array, and as I need to send post parameters in JSON format, I have created its NSDictionary
. My issue is how can I assign object to key in dictionary for Byte
, I tried doing so and application crashes while creating NSDictionary
. I am also explaining each steps with code for easy understanding of y question below :-
Here is my code for converting image to Byte format :-
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data bytes];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
So, now I have Byte
which contain image in byte array, now I want to assign this object to NSDictionary
. Here is code,
NSDictionary *dictJson = [NSDictionary dictionaryWithObjectsAndKeys:
byteData, @"photo",
nil];
Now my application crashes in above line where I am creating dictJson
object, is there any way I can pass Byte to NSDictionary
?
Also, another question, how can I NSLog
byteData
?
Please help me.
Thanks in advance!
If you'll send this information as JSON to a server, you need to convert it first to a valid string using an encoding such as Base 64.
I believe any object you place in an
NSDictionary
must itself be derived fromNSObject
.Clearly
Byte *
is not derived fromNSObject
.Maybe try using a collection class that is, such as
NSArray
, or indeed try putting theNSData
in there directly.Create your Request Dictionary with File Object in it, later postWith: function will manipulate you image binding task
Following Function will read all your parameters from dictionary of objects you want to POST if you want to send image then add "file" key in your dictionary that identifies there is some file to be send with request
If you really want to sent the image data as a JSON array containing the bytes as numbers, then you have to create the array "manually", there is not built-in function:
The JSON data would then look like
So perhaps that is what the server expects. But note that this is a very ineffective (in terms of space) way to send image data (compared to Base64 for example).
You can try putting the bytes into NSValue: