Problem converting image to base64

Hello there, I'm trying to convert an image into base64 encode, however when I try to Upload it to server using Alamofire it failes with no error.

I'm using Xocde 8.3.3 and here is my code.


func submitSale() {

let url : String = "http:/

let imageDataPNG : Data = UIImagePNGRepresentation(UIImage(named: "japanvillage")!)!

let imageDataJPEG : Data = UIImageJPEGRepresentation(UIImage(named: "japanvillage")!, 80)!

let imageStrPNG = imageDataPNG.base64EncodedString()

let imageStrJPEG = imageDataJPEG.base64EncodedString()

let stringBase64 = imageStrJPEG.replacingOccurrences(of: "+", with: "%2B")

Alamofire.request(url, method: .post, parameters: updateInfo, encoding: JSONEncoding.default).responseJSON { response in

...


is there any one could help ?

Actually it's working but with a huge delay! for example an image with 100KB size, takes 30Sec for Alamofire.request to handle that!

a. In your second post, you say it's working, so what problem are you asking about? An upload can take time (and, typically, more time than a download of the same size). So, is there an actual problem? Have you determined that's a problem with Swift, Xcode or iOS, or is it something to do with how Alamofire handles your requests?


b. It's curious that you percent-encode the "+" in your base64 string. If it's being uploaded as data, this shouldn't be necessary. You would need to do it if your string was being submitted as parameters in the URL for your POST request. Since you don't show how the string you construct in "stringBase64" is used, it's not clear how you are using it in the request.


If you're submitting a 100KB URL (if that even works), then it might be expected to be handled less efficiently than as a data attachment.

I have find this similar solution in Python language.

Base64 encoding enables us to convert bytes containing binary or text data to ASCII characters. By encoding the data, we improve the chances of it being processed correctly by various systems. Some systems don’t understand the data in bytes format, so we have to convert the data.

To convert the Image to Base64 String in Python we have to use the Python base64 module that provides b64encode() method. 

The purpose of the base64.b64encode() method in Python is to convert the binary data into ASCII-safe “text”. But Python strongly disagrees with that. Python is now very strict about this that bytes.encode() method doesn’t even exist, and so ‘xyz’.encode(‘base64’) would raise an AttributeError.

Problem converting image to base64
 
 
Q