Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift NSData from base64encoded string returns nil

I'm communicating with a server in Swift retrieving image data. The incoming data is encoded as a base64 string. I am able to correctly receive and display the encoded strings. When I go to use the NSData class to decode the string back to binary data and display...

println(NSData(base64EncodedString: imageString, options: NSDataBase64DecodingOptions(0)))

The output is

nil
nil
nil
nil
nil
nil

One for each of the images received.

I've also tried

println(NSData(base64EncodedString: imageString, options: nil))

and the same results. Is there anything I am missing along the way?? I would put the image strings up but they are massively long...

like image 241
Freestyle076 Avatar asked Jan 14 '15 23:01

Freestyle076


1 Answers

For others that may be having this issue, make sure your Base64 encoded string has a length divisible by 4 (= should be used to pad the length).

See this StackOverflow answer here: https://stackoverflow.com/a/36366421/330494

like image 187
Barlow Tucker Avatar answered Dec 05 '22 03:12

Barlow Tucker