如何从iOS应用程序使用POST发送NSData?

NSData *imageData = UIImagePNGRepresentation(image); 

如何使用POST发送imageData?

下面的代码应该有所帮助

 NSData *imageData = UIImagePNGRepresentation(image); NSURL *yourURL = ... NSMutableURLRequest *yourRequest = [NSMutableURLRequest requestWithURL:yourURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; //Set request to post [yourRequest setHTTPMethod:@"POST"]; //Set content type [yourRequest setValue:@"image/png" forHTTPHeaderField:@"Content-Type"]; // Set authorization header if required ... // set data [yourRequest setHTTPBody:imageData]; // create connection and set delegate if needed NSURLConnection *yourConnection = [[NSURLConnection alloc] initWithRequest:yourRequest delegate:self startImmediately:YES]; 

请注意,假设您正在使用ARC。

你可以检查这个答案,如果你可以使用NSURLConnection https://stackoverflow.com/a/10750428/591951

本文解释了如何POST一个audio文件,但是您可以使用相同的方法上传任何文件

您可以使用ASIHTTPRequest库: http ://allseeing-i.com/ASIHTTPRequest/

那很简单:

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; // Upload an NSData instance [request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"]; 

有关如何使用的信息: http : //allseeing-i.com/ASIHTTPRequest/How-to-use