将图像上传到服务器

时间:2011-08-26 09:20:04

标签: ios objective-c cocoa-touch

有人可以建议一个关于如何以编程方式将图像从我的iPhone应用程序上传到服务器的好教程吗?

3 个答案:

答案 0 :(得分:3)

我会推荐以下网络库

AFNetworking

答案 1 :(得分:0)

我建议您使用ASIHTTP库执行此操作。库附带的示例代码也有适当的示例。该库可以在这里下载:http://allseeing-i.com/ASIHTTPRequest/

答案 2 :(得分:-1)

过程1:

  • 抓住ASIHTTPRequest api并将其添加到您的项目中。请参阅文档here

  • ASIFormDataRequest.h 导入您的班级并加入代表

将其用作

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL  URLWithString:@"upload_url"]];
request.delegate = self;
[request setFile:@"your_image_name.jpg" forKey:@"photo"];
 // or [request setFile:@"your_image_name.jpg" withFileName:@"image_name_for_server.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
[request startAsynchronous];

获取确认实现委托方法,例如

- (void)uploadRequestFinished:(ASIHTTPRequest *)request
{
  NSLog(@"File upload successful"); 
}

- (void)uploadRequestFailed:(ASIHTTPRequest *)request
{ 
  NSLog(@" Error: file upload failed. Issue: \"%@\"",[[request error] localizedDescription]); 
}


N.B: 如果您遇到ARC问题,那么只需转到Xcode的 文件 - >重构 并选择 转换为Objective-C ARC ......

OR

转到 项目 - >目标 - >建造阶段 - >编译源 ,选择文件并双击所选文件。写 -fno-objc-arc


过程2:

这样做的最新方法是使用AFNetworking。 API可以从here

下载


要上传图片,请写

//Create http client
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"/*server_url*/"]];
//convert image to data
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"your_image_name.jpg"], 0.5/*this is compressionQuality, 0.5 will reduce the image into half*/);

// for PNG image, NSData *imageData = UIImagePNGRepresentation(([UIImage imageNamed:@"your_image_name.png"]); // it doesn't support compression

//add image data to the request
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                     path:@"/upload"
                                                               parameters:nil
                                                constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
                                                                           {
                                                                              [formData appendPartWithFileData:imageData 
                                                                                                          name:@"image_name_for_server" 
                                                                                                      fileName:@"image_name_for_server.jpg" 
                                                                                                      mimeType:@"image/jpeg"];
                                                                           }
                              ];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
                                     {
                                       //this where you can monitor your upload progress
                                       NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
                                     }
];

[httpClient enqueueHTTPRequestOperation:operation];