我正在使用这个web服务制作一个web服务和一个iOS应用程序。web服务的API接受带有两个POST变量的HTTP请求:
picture[title]
picturepicture[picture]
的标题图片数据本身现在,对于HTML,这并不是一个问题,因为web浏览器构建了请求,我唯一需要担心的就是enctype="multipart/form-data"
。
但是对于iOS应用程序,我有两个问题,以及我现在要问的两个问题。
如何将图像选择器或摄像机中的JPEG-data?
picture[title]
-field作为纯文本,而picture[picture]
字段必须包含JPEG-data?在服务器端,我使用剪纸宝石,可与PHP的$_FILES
.媲美。我看到了一些上传图片的例子,但这只包括图像,而不是其他后变量。
有谁可以帮我?谢谢。
发布于 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%。而且,为了方便起见,万一链接中断,下面是相关代码(为了更清楚和更好地适应这个问题):
- (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’图片‘。
https://stackoverflow.com/questions/4541304
复制相似问题