0

How to convert UIImage or NSDate to byte array and post to server. In my app i have to convert UIImage to byte array and i have to post.

for post i am using as ASIFormDataRequest.

My code is:

NSUInteger len = [self.dataImage length];
 Byte *byteData= (Byte*)malloc(len);  //converting date to byte array
 [self.dataImage  getBytes:byteData length:len];


 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
  [request setRequestMethod:@"POST"];


 [request addData:self.dataImage withFileName:@"Image.png" andContentType:@"image/png" forKey:@"photo"];
 [request setDelegate:self];
 [request startAsynchronous];
Bhanu
  • 1,249
  • 10
  • 17
user2230971
  • 287
  • 1
  • 7
  • 22
  • possible duplicate of [How to convert NSData to byte array in iPhone?](http://stackoverflow.com/questions/724086/how-to-convert-nsdata-to-byte-array-in-iphone) – Rok Jarc Oct 01 '13 at 10:17

2 Answers2

1

Try Like this,

    NSData *imageData = UIImagePNGRepresentation(self.dataImage ,0.1);
 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
  [request setRequestMethod:@"POST"];


 [request addData:imageData withFileName:@"Image.png" andContentType:@"image/png" forKey:@"photo"];
 [request setDelegate:self];
 [request startAsynchronous];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
0

I solved this by replacing

         [request addData:imageData withFileName:@"Image.png" andContentType:@"image/png" forKey:@"photo"];

with

       [request appendPostData:self.dataImage];
user2230971
  • 287
  • 1
  • 7
  • 22