问题描述
我正在使用此 Web 服务制作 Web 服务和 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:
picture[title]
图片的标题picture[picture]
图片数据本身
picture[title]
the title of the picturepicture[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.
- 如何将图像选择器或相机中的 UIImage 转换为原始 JPEG 数据?
- 如何使用正确的 POST 数据创建 NSURLRequest,该数据必须包含纯文本形式的
picture[title]
字段和带有的picture[picture]
字段JPEG数据?在服务器端,我使用 Paperclip gem,与 PHP 的$_FILES
相当.
- How to convert the UIImage from the image picker or camera to raw JPEG-data?
- How to make the NSURLRequest with the right POST-data, which must contain the
picture[title]
-field as plain text and thepicture[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.
谁能帮帮我?谢谢.
推荐答案
看看这个教程,很不错:
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:@"
--%@
", boundary]
dataUsingEncoding:NSASCIIStringEncoding]];
// add the title
[body appendData:[
@"Content-Disposition: form-data; name="title"
"
dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[title
dataUsingEncoding:NSASCIIStringEncoding]];
// add a boundary to show where the file starts
[body appendData:[[NSString stringWithFormat:@"
--%@
", boundary]
dataUsingEncoding:NSASCIIStringEncoding]];
// add a form field
[body appendData:[
@"Content-Disposition: form-data; name="picture"; filename="image.jpeg"
"
dataUsingEncoding:NSASCIIStringEncoding]];
// tell the server to expect some binary
[body appendData:[
@"Content-Type: application/octet-stream
"
dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[
@"Content-Transfer-Encoding: binary
"
dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[[NSString stringWithFormat:
@"Content-Length: %i
", imageData.length]
dataUsingEncoding:NSASCIIStringEncoding]];
// add the payload
[body appendData:[NSData dataWithData:imageData]];
// tell the server the payload has ended
[body appendData:
[[NSString stringWithFormat:@"
--%@--
", 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 数据一起上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!