NSData *imageData = UIImagePNGRepresentation(image);
如何使用POST发送imageData?
答案 0 :(得分:11)
以下代码应该有帮助
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。
答案 1 :(得分:2)
如果您可以使用NSURLConnection,可以查看此答案 https://stackoverflow.com/a/10750428/591951
本文将介绍如何发布音频文件,但您可以使用相同的方法上传任何文件
答案 2 :(得分:1)
您可以使用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"];