2013-10-01 81 views
0

如何將UIImageNSDate轉換爲字節數組併發布到服務器。 在我的應用程序中,我必須將UIImage轉換爲字節數組,我必須發佈。如何將UIImage或NSData轉換爲字節數組併發布到服務器?

對於我以後使用的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]; 
+0

[如何將NSData轉換爲iPhone中的字節數組?](http://stackoverflow.com/questions/724086/how-to-convert-nsdata-to-byte-array-in-iphone) –

回答

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

我解決了這個替換

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

 [request appendPostData:self.dataImage]; 
相關問題