2015-04-16 156 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];