iPhone UIImage上传到网络服务 [英] iPhone UIImage upload to web service

查看:148
本文介绍了iPhone UIImage上传到网络服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天已经工作了好几个小时了,而且我非常接近解决方案,但显然需要一些人的帮助。我正试图从iPhone发布图像到Web服务。我先发布代码然后解释我尝试过的所有内容:

I worked on this for several hours today and I'm pretty close to a solution but clearly need some help from someone who's pulled this off. I'm trying to post an image to a web service from the iPhone. I'll post the code first then explain everything I've tried:

NSData *imageData = UIImageJPEGRepresentation(barCodePic, .9);


NSString *soapMsg = 
[NSString stringWithFormat:
 @"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><WriteImage xmlns=\"http://myserver/imagewebservice/\"><ImgIn>%@</ImgIn></WriteImage></soap:Body></soap:Envelope>",  [NSData dataWithData:imageData]
 ];


NSURL *url = [NSURL URLWithString:@"http://myserver/imagewebservice/service1.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

NSString *msgLength = 
[NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://myserver/imagewebservice/WriteImage" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    webData = [[NSMutableData data] retain];
}    

首先,此代码适用于除图像之外的任何内容。 Web服务在我的本地网络上运行,我可以随意更改源代码,如果我将ImgIn参数更改为字符串并传入一个字符串,一切正常,我得到一个返回值没问题。所以根本没有连接问题,我能够在这台服务器上调用并从这个Web服务获取数据没有问题。但我需要通过ImgIn参数将图像上传到此Web服务,因此上面的代码是我迄今为止最好的镜头。我也有didReceiveResponse,didReceiveData,didFailWithError等都在处理。上面的代码每次都会触发didRecieveResponse。但是,didReceiveData永远不会被触发,就像Web服务本身甚至从未运行过一样。当我调试Web服务本身时,它在我使用字符串参数时运行和调试很好,但是使用image参数时,它甚至在我调用它时都不会进行调试。这几乎就像ImgIn参数太长了(当我将它输出到屏幕时它是巨大的)并且Web服务只是呛到它。我已经读过在使用这种方法时必须编码到Base64,但我找不到任何关于如何完成的良好链接。如果这就是我做错了,你能不能提供如何做到这一点的代码,而不仅仅是你需要使用Base64,我真的很感激,因为我几乎找不到任何关于如何用例。除此之外,我有点迷茫,似乎我正在做其他一切正确的事情。请帮忙!

First thing, this code works fine for anything but an image. The web service is running on my local network and I can change the source code at will and if I change the "ImgIn" parameter to a string and pass a string in, everything works fine, I get a return value no problem. So there are no connectivity issues at all, I'm able to call and get data from this web service on this server no problems. But I need to upload an image to this web service via the ImgIn parameter, so the above code is my best shot so far. I also have didReceiveResponse, didReceiveData, didFailWithError, etc all being handled. The above code fires off didRecieveResponse every time. However didReceiveData is never fired and it's like the web service itself never even runs. When I debug the web service itself, it runs and debugs fine when I use a string parameter, but with the image parameter, it never even debugs when I call it. It's almost like the ImgIn parameter is too long (it's huge when I output it to the screen) and the web service just chokes on it. I've read about having to encode to Base64 when using this method, but I can't find any good links on how that's done. If that's what I'm doing wrong, can you PLEASE provide code as to how to do this, not just "you need to use Base64", I'd really appreciate it as I can find almost nothing on how to implement this with an example. Other than that, I'm kind of lost, it seems like I'm doing everything else right. Please help!

谢谢

推荐答案

因为%@而失败format specifier接受一个对象并调用 - (NSString *)描述将其转换为字符串。 - [NSData description]为(ASCII)123abc返回类似@< 31 32 33 65 66 67>的内容,因此您提交< 31 32 33 65 66 67>,这显然不是有效的XML。不要将字符串粘合在一起(除了其他东西之外,它还容易受到XML注入)。

It fails because the %@ format specifier takes an object and calls -(NSString*)description to turn it into a string. -[NSData description] returns something like @"<31 32 33 65 66 67>" for (ASCII) "123abc", hence you're submitting <31 32 33 65 66 67> which obviously isn't valid XML. Don't glue strings together (it'll be vulnerable to XML injection, among other things).

我会删除SOAP并使用

I would drop SOAP and just use

[req addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:imgData]

如果你想使用SOAP,那么可能是Objective-C SOAP库,它将为您打包。如果做不到这一点,你可以手动指定xs:base64Binary(我认为)并使用base64编码的数据。这些都不是理想的,因为它使数据大33%。如果你运气不好,一个SOAP库会使它成为 20000%更大

If you want to use SOAP, then there's probably an Objective-C SOAP library which will do packaging for you. Failing that, you can manually specify xs:base64Binary (I think) and use base64-encoded data. Neither of these is ideal, since it makes the data 33% larger. If you're unlucky, a SOAP library will make it 20000% larger!

这篇关于iPhone UIImage上传到网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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