【问题标题】:Send UIImageView as parameter to WCF service将 UIImageView 作为参数发送到 WCF 服务
【发布时间】:2014-03-14 17:28:42
【问题描述】:

我已经搜索了很多关于如何从 iOS 向我的 WCF 服务发送 UIImageView 的信息,知道我使用的是 xcode5。

你能帮我想办法解决它吗?您可以在下面找到我为解决问题所做的工作,但我无法用它来解决。

首先我创建了一个接受字符串作为参数的 WCF 服务:

服务.cvs

public string InsertNewImage(string imageEncoded) {
     //I added the method to convert the imageEncoded from base64 
     //Then insert the image in sql server DB.
}

IService.cs:

[OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/InsertNewImage/{id1}")]
    string InsertNewImage(string id1);

在我的 iOS 代码中,我实现了一个按钮来调用我的网络服务,如下所示:

 //Encode my UIIMage

-(NSString *)encodeToBase64String:(UIImage *)image {
    return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
 }

//assign the Encode method result

NSString *imageStringEncoded = encodeToBase64String(myUIIMage);

    NSString *str= @"http://serverIP/iOS/Service.svc/json/";
    str=[str stringByAppendingFormat:@"InsertNewImage/%@" , imageStringEncoded];
    str=[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *WcfSeviceURL = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:WcfSeviceURL];

    [request setHTTPMethod:@"POST"];

    NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

你能帮我用这种方法还是建议我用另一种方法来做到这一点。

谢谢

【问题讨论】:

  • 我不知道这个问题的解决方案,但是你的 http url 不应该超过 2000 个字符。 stackoverflow.com/questions/417142/…
  • 好的,感谢您提供的信息,请问如何发送图片?

标签: ios objective-c wcf uiimageview xcode5


【解决方案1】:

我通过使用另一种发送流文件的方式解决了这个问题。 以下是我所做的:

我创建了接受参数作为流的 WCF 服务:

服务.svc

 public void SendFile(Stream img)
    {
        byte[] buffer = new byte[10000];
        img.Read(buffer, 0, 10000);
        FileStream f = new FileStream("D:\\sample.jpg", FileMode.OpenOrCreate);
        f.Write(buffer, 0, buffer.Length);
        f.Close();
        img.Close();

    }

Iservice.cd

 [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = @"/FileUploaded/")]
    void SendFile(Stream img);

在 iOS 源代码中,我在单击按钮时添加了以下代码。

UIImage *image = [UIImage imageNamed:@"myImage.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 90);

NSString *urlString = @"http://serverAddress.com/iOS/myService.svc/FileUploaded";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *postBody = [NSMutableData data];

[postBody appendData:[NSData dataWithData:imageData]];

[request setHTTPBody: postBody];

NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil   error:nil];
NSString *respStr = [[NSString alloc] initWithData:respData  encoding:NSUTF8StringEncoding];

希望这对某人有所帮助。

谢谢

【讨论】:

    猜你喜欢
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多