2015-04-16 192 views
1

在我的IOS應用程序中,我想使用HTTP請求將圖像和圖像名稱一起發送到服務器。
我是一個嵌入式背景的程序員,所以沒有意識到使用HTTP調用,而且對於iPhone開發來說也很新穎。從IOS發送映像到服務器

我該如何做到這一點,任何示例代碼或教程將不勝感激。

+0

您可以使用AFNetworking框架從移動設備發送圖片。檢查這個[鏈接](http://stackoverflow.com/questions/16692102/send-image-along-with-other-parameters-with-afnetworking) – aykutt

回答

0

從我的角度來看,最簡單的方法是使用AFNetworking

它會將圖像發送到一個php頁面,然後在服務器上,您可以使用發送的數據構建並保存圖像。

Here's一個基本的教程,但很多人在互聯網上。

0

爲此,您可以通過使用ASIHTTPRequest

NSURL *strUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@?action=youraction",serverUrl]]; 
ASIFormDataRequest *request; 
request = [[[ASIFormDataRequest alloc] initWithURL:strUrl] autorelease]; 
[request setRequestMethod:@"POST"]; 
[request setTimeOutSeconds:120]; 

NSString *imagePAth = userImagePath; 

NSArray *imageName = [userImagePath componentsSeparatedByString:@"/"]; 

if(userImagePath) 
{ 
    [request setFile:imagePAth withFileName:[imageName lastObject] andContentType:@"image/jpeg"     forKey:@"profileImage"]; 
} 

[request setUseCookiePersistence:NO]; 
[request setUseSessionPersistence:NO]; 
[request setDelegate:self]; 

[request setDidFinishSelector:@selector(requestFinished:)]; 
[request setDidFailSelector:@selector(requestFailed:)]; 

[request startAsynchronous]; 

- (void)requestFinished:(ASIHTTPRequest *)request 

{ 

} 

- (void)requestFailed:(ASIHTTPRequest *)request 
{ 

} 
+0

哪些頭文件我需要包括 – Sarao

+0

#import「ASIFormDataRequest.h」 –

+0

我認爲#import「ASIFormDataRequest.h」在ios8中不起作用 – Sarao

1

的更好的方法是使用Image Compress Library Here首先壓縮的圖像,然後上傳它的使用和網絡圖書館Liek AF網絡做這樣的事情,或者你也可以把它使用NSUrlConnection。 AFNetworking易於使用。您可以訪問this page以瞭解如何將其導入到您的項目中。寫下這些代碼行。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; 
[manager POST:@"http://samwize.com/api/poo/" 
    parameters:@{@"color": @"green"} 
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
    [formData appendPartWithFileURL:filePath name:@"image" error:nil]; 
} success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Success: %@", responseObject); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 
0

使用NSURLConnection的,請確保您圖像轉換成NSData的 USER_ID和關鍵有參數。

NSURL *URL = [NSURL URLWithString:constFileUploadURL]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; 
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[request setHTTPShouldHandleCookies:NO]; 
[request setTimeoutInterval:60]; 
[request setHTTPMethod:@"POST"]; 
NSString *boundary = @"0xLhTaLbOkNdArZ"; 

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
[request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
NSString *userid = [NSString stringWithFormat:@"%li",userID]; 
[body appendData:[userid dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 


[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[constBackendKey dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

for (NSData *data in arrayWithFiles) 
{ 
     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files%ld\"; filename=\"image%ld.jpg\"\r\n",[arrayWithFiles indexOfObject:data],[arrayWithFiles indexOfObject:data]] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[NSData dataWithData:data]]; 
     [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
} 

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setHTTPBody:body]; 
_connection = [NSURLConnection connectionWithRequest:request delegate:self];