从IOS向服务器发送图像

从IOS向服务器发送图像,ios,objective-c,http,Ios,Objective C,Http,在我的IOS应用程序中,我想使用HTTP请求将图像和图像名称一起发送到服务器。 我是一名具有嵌入式背景的程序员,因此不了解HTTP调用,对iPhone开发也很陌生 我如何才能做到这一点,任何样本代码或教程将不胜感激 在我看来,最简单的方法就是使用 它会将图像发送到一个php页面,然后在服务器上,使用发送的数据构建并保存图像 这是一个基础教程,但在互联网上还有很多其他教程。为此,您可以使用 更好的方法是先使用压缩图像,然后使用网络库Liek AF Networking上传图像,或者也可以使用NSU

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


我如何才能做到这一点,任何样本代码或教程将不胜感激

在我看来,最简单的方法就是使用

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


这是一个基础教程,但在互联网上还有很多其他教程。

为此,您可以使用


更好的方法是先使用压缩图像,然后使用网络库Liek AF Networking上传图像,或者也可以使用NSUrlConnection发送图像。AFN网络很容易使用。您可以访问以了解如何将其导入到您的项目中。写下这些代码行

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);
}];
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager];
NSURL*filePath=[NSURL fileURLWithPath:@”file://path/to/image.png"];
[经理职务:@”http://samwize.com/api/poo/"
参数:@{@“颜色”:@“绿色”}
构造体WithBlock:^(id formData){
[formData appendPartWithFileURL:filePath名称:@“image”错误:nil];
}成功:^(AFHTTPRequestOperation*操作,id响应对象){
NSLog(@“成功:%@”,响应对象);
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“错误:%@”,错误);
}];

使用NSURLConnection,确保将图像转换为NSData 用户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];

您可以使用AFNetworking框架从手机发送图片。检查我需要包含哪些头文件#import“ASIFormDataRequest.h”它可以工作,但您必须从我提供的git链接添加ASIHTTPRequest的所有文件。还需要添加一些框架。
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];