user2230971
user2230971

Reputation: 287

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

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

Upvotes: 0

Views: 2073

Answers (2)

user2230971
user2230971

Reputation: 287

I solved this by replacing

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

with

       [request appendPostData:self.dataImage];

Upvotes: 0

Vinodh
Vinodh

Reputation: 5268

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

Upvotes: 1

Related Questions