【发布时间】:2011-02-23 12:19:33
【问题描述】:
我今天为此工作了几个小时,我非常接近解决方案,但显然需要有人帮助解决这个问题。我正在尝试将图像从 iPhone 发布到网络服务。我将首先发布代码,然后解释我尝试过的所有内容:
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 参数将图像上传到这个网络服务,所以上面的代码是我迄今为止最好的镜头。我还处理了 didReceiveResponse、didReceiveData、didFailWithError 等。上面的代码每次都会触发 didRecieveResponse。但是 didReceiveData 从未被触发,就像 Web 服务本身甚至从未运行一样。当我调试 Web 服务本身时,当我使用字符串参数时它可以正常运行和调试,但使用 image 参数时,它甚至在我调用它时都无法调试。这几乎就像 ImgIn 参数太长(当我将它输出到屏幕时它很大)并且 Web 服务只是阻塞它。我读过有关在使用此方法时必须编码为 Base64 的信息,但我找不到任何关于如何完成的好的链接。如果那是我做错的,您能否提供有关如何执行此操作的代码,而不仅仅是“您需要使用 Base64”,我真的很感激,因为我几乎找不到任何关于如何用例子。除此之外,我有点迷茫,似乎我做的一切都是正确的。请帮忙!
谢谢
【问题讨论】:
标签: iphone image upload service