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

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

问题描述

我今天为此工作了几个小时,我已经很接近解决方案了,但显然需要有人帮助解决这个问题.我正在尝试将图像从 iPhone 发布到网络服务.我会先发布代码,然后解释我尝试过的一切:

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 服务本身时,当我使用字符串参数时它可以正常运行和调试,但是使用图像参数时,它甚至在我调用它时都不会调试.这几乎就像 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!

谢谢

推荐答案

失败是因为 %@ 格式说明符接受一个对象并调用 -(NSString*)description 将其转换为字符串.-[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天全站免登陆