如何将UIImage或NSData转换为字节数组并发布到服务器?

时间:2013-10-01 10:15:08

标签: ios post bytearray nsdata asiformdatarequest

如何将UIImageNSDate转换为字节数组并发布到服务器。 在我的应用程序中,我必须将UIImage转换为字节数组,我必须发布。

我正在使用的ASIFormDataRequest

我的代码是:

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 个答案:

答案 0 :(得分:1)

尝试这样,

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

答案 1 :(得分:0)

我通过替换

解决了这个问题
         [request addData:imageData withFileName:@"Image.png" andContentType:@"image/png" forKey:@"photo"];

       [request appendPostData:self.dataImage];