首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >将图像从UIImage上传为JPEG和其他后置数据。

将图像从UIImage上传为JPEG和其他后置数据。
EN

Stack Overflow用户
提问于 2010-12-27 11:42:19
回答 1查看 5.5K关注 0票数 4

我正在使用这个web服务制作一个web服务和一个iOS应用程序。web服务的API接受带有两个POST变量的HTTP请求:

  1. picture[title] picture
  2. picture[picture]的标题图片数据本身

现在,对于HTML,这并不是一个问题,因为web浏览器构建了请求,我唯一需要担心的就是enctype="multipart/form-data"

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

如何将图像选择器或摄像机中的JPEG-data?

  • How转换为原始,使NSURLRequest具有正确的后数据,其中必须包含picture[title]-field作为纯文本,而picture[picture]字段必须包含JPEG-data?在服务器端,我使用剪纸宝石,可与PHP的$_FILES.媲美。

我看到了一些上传图片的例子,但这只包括图像,而不是其他后变量。

有谁可以帮我?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2011-06-22 17:26:05

请看本教程,它非常不错:

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

有一点不太正确,那就是它将90作为质量设置传递给UIImageJPEGRepresentation,它实际上将质量作为浮动在0.0到1.0之间,因此应该是0.9。我确信代码是有效的,它只是指定100%的质量,而不是按照预期的90%。而且,为了方便起见,万一链接中断,下面是相关代码(为了更清楚和更好地适应这个问题):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (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);
}

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

在PHP中,您应该看到设置了$_REQUEST‘’title‘和$_FILES’图片‘。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4541304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文