How to convert UIImage or NSData to byte Array and post to server?

2k views Asked by At

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];
2

There are 2 answers

0
Vinodh On

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];
0
user2230971 On

I solved this by replacing

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

with

       [request appendPostData:self.dataImage];