1

I want to generate Base64 string for a specific image. for that I've wrote below code

let imageData = UIImagePNGRepresentation(imgProfile.image!)!
var imageStr = imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)

and I'm getting this output string as Base64 which is not decoded (i.e. wrong).

but when I'm generating Base64 from here, I'm getting this output string, which is successfully decoded and getting the image back (i.e. correct)

Please help me to find the issue.
Thanks in advance

I've already visited following threads.
1. https://stackoverflow.com/a/47610733/3110026
2. https://stackoverflow.com/a/46309421/3110026

Mrugesh Tank
  • 3,495
  • 2
  • 29
  • 59
  • Tried with `data.base64EncodedString()` only not with options? – TheTiger Mar 19 '18 at 13:30
  • The string is properly encoded. You are adding CRLF (`\n\r`) after each 64 characters with the options you passed. What do you expect? To decode the string ignoring the new lines you have to pass `.ignoreUnknownCharacters`. – vadian Mar 19 '18 at 13:31
  • @vadian I'm not adding any CRLF characters. as you can see there is "Pwq5Uhn1T836" string before AAAAA at the end of string on original, but there is no like that string in my generated string – Mrugesh Tank Mar 19 '18 at 13:37
  • You do add CRLF characters. The option `lineLength64Characters` does that. The text in your [link](http://freeonlinetools24.com/base64-image) contains a lot of them. Please see my answer. – vadian Mar 19 '18 at 13:39
  • @TheTiger thank you for help. got original image – Mrugesh Tank Mar 19 '18 at 13:46

3 Answers3

1

Additional, I was getting the same problem, but for me \r\n was not the issue, when i get the base64 string from server there were blank spaces between some characters and after comparing it with correct Base64 string what i found is that i have to add "+" sign in blank spaces. When I did that...Bingooooo...... "yourBase64String" can contain \r\n..

if let cleanImageString =  yourBase64String.replacingOccurrences(of: " ", with: "+") {
        if let data = Data(base64Encoded: cleanImageString, options: .ignoreUnknownCharacters) {
            yourImageView.image = UIImage(data: data)
        }
    }
iOSDev456
  • 11
  • 3
0

try below UIImage extension:

extension UIImage {



    /// Encoded Base64 String of the image
    var base64: String? {
        guard let imageData = UIImageJPEGRepresentation(self, 1.0) as NSData? else {
            print("Error occured while encoding image to base64. In \(self), \(#function)")
            return nil
        }
        return imageData.base64EncodedString()
    }


}
Moayad Al kouz
  • 1,342
  • 1
  • 9
  • 19
0

The string is properly encoded. You are adding CRLF (\n\r) after each 64 characters with the options you passed.

The simplest solution is to pass no options

let imageData = UIImagePNGRepresentation(imgProfile.image!)!
let imageStr = imageData.base64EncodedString()

Or decode the data with options ignoreUnknownCharacters

let imageData = UIImagePNGRepresentation(imgProfile.image!)!
let imageStr = imageData.base64EncodedString(options: .lineLength64Characters)
...
if let data = Data(base64Encoded: imageStr, options: .ignoreUnknownCharacters) { ...
vadian
  • 274,689
  • 30
  • 353
  • 361