Ad

Our DNA is written in Swift
Jump

How to send image to remote server

Somebody asked on StackOverflow:

how to send image to Remote server from the below code last string is placed with image

NSString *reqString=[NSString stringWithFormat:@”http://projeceads.info/spir/productinfo/productadd/%@/%@/%@/%@/%@/%@/%@/%@”,ownerId,productNameTxt.text,QuantityTxt.text,sizeTxt.text,ageTxt.text,priceTxt.text,descriptionView.text,imgstr]; NSURL *reqUrl=[NSURL URLWithString:reqString]; NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:reqUrl] delegate:self];
[connection start]; }

Now there are several things wrong with this question. This is the very first question this person asked on StackOverflow and he didn’t even set a proper user name yet. The code is not formatted as such, the English is bad and to most casual onlookers it is unclear what this question is about.

Still I felt an urge to give a good answer because often it is non-sensical questions like this that challenge our ability to parse the intent of the asking person and our knowledge of the subject matter. It is also such a situation where you can give an answer that exactly matches the question as well as a second answer that is a better way of achieving the intent.

There are two ways how you can upload data to a web server: GET or POST

With GET you have to put all information that you want the web server to have into the URL. With POST you additionally can put information into the POST body.

From your question I deduct that he seems to try to upload the image via a GET request and thus needs to “append the image to the URL”.

An image as such is binary data, either uncompressed as RGBA values, usually 1 byte each times width times height. Since this is too much data for most uses, people like to compress images. UIImagePNGRepresentation allows you to create the data in PNG or JPEG format.

But even this compressed format is might not transferable via HTTP if your webserver is unable to process binary data. Historically you have not been able to use all values a byte can hold (0-255) in HTTP which itself is a pure text format. Originally HTTP was only using 7-bit ASCII characters (0-127) which are safe to be transmitted. Because of this any binary data you want to send somewhere has to be encoded into a representation that only uses safe characters. The most prevalent such encoding scheme is “base64”.

One drawback of base64 encoding is that it increases the amount of data you need to send by about a third. Also most modern web servers can actually receive binary post data which is why in the following article I did not need to encode the image. I wrote up here how to upload an image via POST to TwitPic. You need to put together the correct headers and compose a POST body as shown here: http://www.cocoanetics.com/2010/02/uploading-uiimages-to-twitpic/

This is the way I would recommend you upload your image as well, if your web server supports it. This is not the answer you are looking for, but it is the approach you SHOULD take.

Now to actually answer your question “how do I create a URL that includes a representation of an image” (because my server admin is too stupid to build me a script that would also accept a POST)…

The steps are:

  1. Create a compressed representation with UIImage…Representation
  2. Base64-encode the output from 1
  3. URL-encode the output from 2
  4. append to URL string
  5. send GET request

For base64-encoding I use a method created by famous Matt Gallagher. URL-encoding is necessary because even less characters are legal to use in well-formed URLs than are in 7-bit HTTP. For URL-encoding I use this NSString category.

Summary

Even though it is possible you should not send images via GET URLs. Send them like normal people as POST requests and put the image data into the POST body in either binary or base64-encoded format.

The steps are:

  1. Create a compressed representation with UIImage…Representation
  2. Base64-encode the output from 1 (optional, if your web server doesn’t support binary or you want to be safe)
  3. construct content headers
  4. append POST body
  5. send POST request

PS: Of course you can also use one of the many networking frameworks out there for constructing POST requests. But I can guarantee that you will not find a single one that will allow your to append a textual-doubly-encoded image representation to the GET URL.


Categories: Q&A

2 Comments »