1

What is the best way to upload image files to web server in java considering IOS9 and swift 2.

Upon searching, I realised that similar question was asked and many different methods are available online to upload images. However I am not sure on what is the best method to follow.

I tried the following approaches and noticed that it takes about a minute to upload an image.

****Approach 1:****

Using NSURLSession and invoking “POST” method of the java servlet Send as JSON as stated in this example

Append image to httpbody itself as stated in this example

****Approach 2:****

Using NSURL and invoking a javascript method as in this example

I also read about AFNetworking 2.0 (ex) and I am not sure if it can bring any performance increase to the uploading process.

Our users might take around 100 of pictures everyday and the images needs to be uploaded to web server for further processing.

Please review and suggest your views. Many thanks.

Community
  • 1
  • 1
PK20
  • 1,066
  • 8
  • 19

2 Answers2

1

I actually work on a social app and to upload a photo on the API i personally use the base64.

Simply i encode the image, so i have a encoded string and the API decoded and put it on a AWS.

I am on Swift 2.0 and iOS 9. If you want a piece of code :

// You create a NSData from your image
var imageData = UIImageJPEGRepresentation(imageURL.image!, 0.5)
// You create a base64 string
let base64String = imageData!.base64EncodedStringWithOptions([])
// And you encode it in order to delete any problem of specials char
let encodeImg = base64String.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) as String!
nekflamm
  • 131
  • 12
  • Even I have used this approach and It seems fine. – Bhaumik Desai Oct 21 '15 at 10:36
  • @Nekfeu can you suggest how your API decoded the image sent. Thanks in advance. – PK20 Oct 22 '15 at 03:12
  • The API do a base64 decode on the string received. The buffer result is put on a JPG and storage in AWS – nekflamm Oct 22 '15 at 08:09
  • I'm currently looking at different approaches to achieve the same . Has any of you out there tried to compare the speeds and efficiency of these approaches : (1). Get UIImage from UIImagePickerController and convert to NSData, then upload the NSData (2)Get UIImage from UIImagePickerController , then convert to PHAsset . then get NSData and upload (3) Get UIImage from UIImagePickerController and upload directly – zulkarnain shah Mar 14 '16 at 12:01
1

Create base64 String out of your image and send it to web server.

let imageData = UIImageJPEGRepresentation(imageURL.image!, 0.5)
let base64String = imageData!.base64EncodedStringWithOptions([])

In your web server, read the input image string and parse it as follows.

byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);

Then you can do whatever you like with the bytes like:

BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
Void
  • 51
  • 3