2012-06-14 140 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的貶值,因爲我以前沒有聽說過。 –