0

I have searched a lot on how to send an UIImageView to my WCF service from iOS, knowing that I work with xcode5.

can you please Help me to find a way to sort it out. you can find below what I did to solve the issue but I cannot solve it with it.

first I created a WCF service that accepts a string as parameter:

Serice.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);

and in my iOS code, I implemented a button to call my web service as below :

 //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];

can you please help me with this approach or suggest on me another way to do that.

thank you

JMarsh
  • 934
  • 7
  • 19
  • 1
    I don't know the solution to this but, you should not have an http url more than 2000 characters. http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers – santhu Mar 14 '14 at 17:38
  • ok, thanks for the info, so how can I send the image please ? – Yassine Khabbazia Mar 14 '14 at 17:43

1 Answers1

0

I have solved the issue by working with another way that consist to send a Stream file. below is what I did:

I created the WCF service that accepts a parameter as a stream:

service.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);

and in the iOS source, I added the below code when a button clicked.

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];

Hope this help someone.

Thank you