从IOS向服务器发送图像

时间:2015-04-16 09:47:23

标签: ios objective-c http

在我的IOS应用程序中,我想使用HTTP请求将图像与图像名称一起发送到服务器 我是一名具有嵌入式背景的程序员,因此不了解HTTP调用,也是iPhone开发的新手。

如何实现这一点,任何示例代码或教程都将受到赞赏。

4 个答案:

答案 0 :(得分:2)

更好的方法是首先使用Image Compress Library Here压缩图像,然后使用网络库Liek AF Networking上传它,或者也可以使用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);
}];

答案 1 :(得分:0)

从我的观点来看,最简单的方法是使用AFNetworking

它将图像发送到php页面,然后在服务器上,您使用发送的数据构建并保存图像。

Here's一个基础教程,但很多其他人在互联网上。

答案 2 :(得分: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
{

}

答案 3 :(得分:0)

使用NSURLConnection,确保将图像转换为NSData user_id和key有参数。

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