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

5
  • Tried with data.base64EncodedString() only not with options? Commented Mar 19, 2018 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. Commented Mar 19, 2018 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 Commented Mar 19, 2018 at 13:37
  • You do add CRLF characters. The option lineLength64Characters does that. The text in your link contains a lot of them. Please see my answer. Commented Mar 19, 2018 at 13:39
  • @TheTiger thank you for help. got original image Commented Mar 19, 2018 at 13:46

3 Answers 3

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)
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

yourBase64String.replacingOccurrences(of: "\r\n", with: "") fixed it for me
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) { ...

Comments

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()
    }


}

3 Comments

Is anything different than converting from png to jpeg? Cause I've already tried that
yup, based on image type pong or jpg
Answers are more helpful if they explain the problem and the solution, instead of dumping code only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.