将图像从UIImage上载为JPEG以及其他POST数据 [英] Uploading an image from a UIImage as a JPEG together with other POST-data

查看:107
本文介绍了将图像从UIImage上载为JPEG以及其他POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此网络服务制作网络服务和iOS应用。 Web服务的API接受带有两个POST变量的HTTP POST请求:

I am making a web service and an iOS app using this web service. The web service's API accepts a HTTP POST request with two POST-variables:


  1. picture [title] 图片标题

  2. 图片[图片] 图片数据本身

  1. picture[title] the title of the picture
  2. picture[picture] the picture data itself

现在使用HTML这不是问题,因为Web浏览器构造请求,我唯一需要担心的是 enctype =multipart / form-data

Now with HTML this is not a problem as the web browser constructs the request and the only thing I have to worry about is enctype="multipart/form-data".

但是对于iOS应用程序,我有两个问题,以及我现在要问的两个问题。

But with the iOS app I have two problems, together with two questions I am going to ask right now.


  1. 如何将UIImage从图像选择器或相机转换为原始JPEG数据?

  2. 如何使用正确的POST数据制作NSURLRequest,其中必须包含图片[title] -field作为纯文本和图片[图片] 包含JPEG数据的字段?在服务器端,我使用Paperclip gem,与PHP的 $ _ FILES 相当。

  1. How to convert the UIImage from the image picker or camera to raw JPEG-data?
  2. How to make the NSURLRequest with the right POST-data, which must contain the picture[title]-field as plain text and the picture[picture] field with the JPEG-data? On the server-side I use the Paperclip gem, comparable with PHP's $_FILES.

我已经看过一些上传图片的例子,但这只包括图片,而不是其他的POST变量。

I have seen some examples of uploading an image, but this only includes the image, not other POST-variables.

任何人都可以帮助我吗?谢谢。

Can anyone help me? Thanks.

推荐答案

查看本教程,这非常好:

Check out this tutorial, which is pretty nice:

http://iphone.zcentric。 com / 2008/08/29 / post-a-uiimage-to-the-web /

有一点不太正确的是它将90作为质量设置传递给UIImageJPEGRepresentation,它实际上将质量视为介于0.0和1.0之间的浮点数,因此应该为0.9。我确信代码有效,它只是指定100%质量而不是90%。并且,为了方便起见,如果链接断开,这里是相关代码(为了清晰起见而重构并更好地适应问题):

One thing that's not quite right about it is that it passes 90 as a quality setting to UIImageJPEGRepresentation, which actually takes quality as a float between 0.0 and 1.0, so that should be 0.9 instead. I'm sure the code works, it's just specifying 100% quality instead of 90% as probably intended. And, just for convenience and in case that link breaks, here's the relevant code (refactored for clarity and to better fit the question):

- (void)uploadImage:(UIImage *)image toURL:(NSURL *)url withTitle:(NSString *)title {

  // encode the image as JPEG
  NSData *imageData = UIImageJPEGRepresentation(image, 0.9);

  // set up the request
  NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
  [request setURL:url];

  // create a boundary to delineate the file
  NSString *boundary = @"14737809831466499882746641449";
  // tell the server what to expect
  NSString *contentType = 
    [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
  [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

  // make a buffer for the post body
  NSMutableData *body = [NSMutableData data];

  // add a boundary to show where the title starts
  [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] 
    dataUsingEncoding:NSASCIIStringEncoding]];

  // add the title
  [body appendData:[
    @"Content-Disposition: form-data; name=\"title\"\r\n\r\n"
    dataUsingEncoding:NSASCIIStringEncoding]];
  [body appendData:[title
    dataUsingEncoding:NSASCIIStringEncoding]];

  // add a boundary to show where the file starts
  [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] 
    dataUsingEncoding:NSASCIIStringEncoding]];

  // add a form field
  [body appendData:[
    @"Content-Disposition: form-data; name=\"picture\"; filename=\"image.jpeg\"\r\n"
    dataUsingEncoding:NSASCIIStringEncoding]];

  // tell the server to expect some binary
  [body appendData:[
    @"Content-Type: application/octet-stream\r\n"
    dataUsingEncoding:NSASCIIStringEncoding]];
  [body appendData:[
    @"Content-Transfer-Encoding: binary\r\n"
    dataUsingEncoding:NSASCIIStringEncoding]];
  [body appendData:[[NSString stringWithFormat:
    @"Content-Length: %i\r\n\r\n", imageData.length]
    dataUsingEncoding:NSASCIIStringEncoding]];

  // add the payload
  [body appendData:[NSData dataWithData:imageData]];

  // tell the server the payload has ended
  [body appendData:
    [[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] 
    dataUsingEncoding:NSASCIIStringEncoding]];

  // add the POST data as the request body
  [request setHTTPMethod:@"POST"];
  [request setHTTPBody:body];

  // now lets make the connection to the web
  NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

  NSLog(@"%@", returnString);
}

我实际上并没有编写它,因为它写在这里,所以它可能包含一些问题。

I haven't actually compiled it as it's written here, so it probably contains some issues.

在PHP中,您应该看到$ _REQUEST ['title']和$ _FILES ['picture']已设置。

In PHP, you should see that $_REQUEST['title'] and $_FILES['picture'] are set.

这篇关于将图像从UIImage上载为JPEG以及其他POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆