我需要使用发布请求将图像发送到我的服务器。我已经为它编写了所需的 PHP 代码。但是我必须如何从我的设备发送它?我见过 ASIHTTPRequest 框架,但它已经过时了,而且 AFNetworking 使事情变得复杂。如果可以的话,我想通过 GET 请求发送图像,但我只是不知道如何。
问问题
510 次
1 回答
0
我创建了一个子类,它允许您创建简单的异步 POST 请求,可以在这里找到:https ://github.com/MaxKDevelopment/MKNetwork 。
要使用它上传图片,您需要做的就是将图片数据作为参数添加到请求中:
[MKNetwork sendAsynchronousRequest:[NSURL URLWithString:@"url to upload to"] params:[NSDictionary dictionaryWithObject:UIImagePNGRepresentation(imagedata) forKey:@"image"] decodeResponse:YES callback:^(id response, NSError *error) {
[someObject doSomethingWithResponse:response];
}];
或者,您可以查看 ASIHTTPRequest (ASIFormDataRequest):http ://allseeing-i.com/ASIHTTPRequest/
然后使用设置为 UIImage 的 POST 值初始化 ASIFormDataRequest 变量。
NSURL *url = [NSURL URLWithString: @"url to upload to];
ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addPostValue:UIImagePNGRepresentation(imageToUpload) forKey:@"image"];
[request setCompletionBlock:^{NSLog(@"completed");}];
[request setFailedBlock:^{NSLog(@"failed");}];
[request startAsynchronous];
于 2012-06-14T20:46:19.437 回答