0

I am trying to upload an image to server by converting the image to base64 encoded string. Below is the conversion code:

let imageData:NSData = UIImagePNGRepresentation(self.selImage!)! as NSData

var base64String = imageData.base64EncodedString(options:NSData.Base64EncodingOptions(rawValue: 0))

self.panBaseStr = base64String



I am getting an error from server as :

Payload is too high

It is due to a large base64 string.

And when I try to print the base64 encoded string, it takes too much time. Any specific reason for this?

How can I downscale the encoded string?

I running this code on simulator and XCODE version is 8 and using Alamofire for API calls.

Please note: The requirement is to send image as an encoded string to server rather than the image file.


Community
  • 1
  • 1
Rakesh Gujari
  • 151
  • 1
  • 8
  • I am using Alamofire for API calls, and I am sending the base64 string as a post param in the call. – Rakesh Gujari Feb 25 '17 at 06:43
  • It sounds like your base64 string is too large for the server. If your server can accept zipped content, then you may be able to zip the string first, otherwise send smaller images – Paulw11 Feb 25 '17 at 07:01

2 Answers2

0

let strBase64 = imageData.base64EncodedStringWithOptions(.allZeros)

this link may be helps you Convert between UIImage and Base64 string

Community
  • 1
  • 1
Chetan Hedamba
  • 229
  • 2
  • 7
  • Nope! that gives the same error. I have already tried it. The ".allZeroes" option is not available in swift 3 NSdata.Base64EncodingOptions – Rakesh Gujari Feb 25 '17 at 07:36
0

Yes you can downscale it by using UIImageJPEGRepresentation instead of UIImagePNGRepresentation.

PNG uses lossless compression so that the image will be exactly the same as the original image. JPEG uses lossy compression, the resulting image is an approximation to the original. Even if JPEG is also offering medium guarantee of your image quality. So you can try out the below code instead of yours

    UIImageJPEGRepresentation(self.selImage, 1.0)   // QUALITY min = 0 / max = 1
arunjos007
  • 4,105
  • 1
  • 28
  • 43