2012-06-14 139 views
0

我需要使用发布请求将图像发送到我的服务器。我已经为它写了所需的PHP代码。但我如何从我的设备发送它?我见过ASIHTTPRequest框架,但它已经过时了,AFNetworking使事情变得复杂。如果我可以通过GET请求发送图片,但我不知道如何。如何在iOS中使用POST请求发送图像

+0

检查了这一点:http://stackoverflow.com/questions/7738704/sending-multipart-post-from-ios-and-reading-parameters-in-php-post一般情况下,你在做什么试图做的是提交一个类型为'multipart/form-data'的数据块的POST。这可能是您的Google搜索字词。 –

回答

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/

然后初始化一个ASIFormDataRequest变量,其POST值设置为UIImage。

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]; 
+1

据我所知ASIHTTP不再支持,它已过时? –

+0

-1用于提示ASI,它已被弃用,并且有更好的选择,如RESTkit或AFNetworking。 –

+0

我也提供了ASIHTTPRequest的替代方案。请你给我一个链接到文章,概述ASI的贬值,因为我以前没有听说过。 –