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
这篇文章解释了如何发布音频文件,但您可以使用相同的方法上传任何文件
您可以使用 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