redamakarem
redamakarem

Reputation: 325

Swift base64 decoding returns nil

I am trying to decode a base64 string to an image in Swift using the following code:

let decodedData=NSData(base64EncodedString: encodedImageData, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)

Unfortunately, the variable decodedData turns out to have a value of nil

Debugging through the code, I verified that the variable encodedImageData is not nil and is the correct encoded image data(verified by using an online base64 to image converter). What could possibly be the reason behind my issue?

Upvotes: 21

Views: 14696

Answers (8)

Daniel Klöck
Daniel Klöck

Reputation: 21157

You can use this extension to make sure that the string has the correct length fro decoding with Foundation (divisible by 4):

extension String {
    var paddedForBase64Decoding: String {
        appending(String(repeating: "=", count: (4 - count % 4) % 4))
    }
}

Usage:

Data(base64Encoded: base64String.paddedForBase64Decoding)

Upvotes: 0

Zaid Pathan
Zaid Pathan

Reputation: 16820

This helped me:

extension String {
    func fromBase64() -> String? {
        guard let data = Data(base64Encoded: self.replacingOccurrences(of: "_", with: "="), options: Data.Base64DecodingOptions(rawValue: 0)) else {
            return nil
        }

        return String(data: data, encoding: .utf8)
    }
}

Usage:

print(base64EncodedString.fromBase64())

Upvotes: 1

Md Imran Choudhury
Md Imran Choudhury

Reputation: 10057

It's make problem with special character, but an interesting point is if we use NSData and NSString then it's working fine.

static func decodeBase64(input: String)->String{
        let base64Decoded = NSData(base64Encoded: input, options:   NSData.Base64DecodingOptions(rawValue: 0))
            .map({ NSString(data: $0 as Data, encoding: String.Encoding.utf8.rawValue) })

        return base64Decoded!! as String
}

Upvotes: 0

Joule87
Joule87

Reputation: 651

Check the content of your decodedData variable and look for this prefix "data:image/png;base64", I had this issue and noticed that my String base64 had a prefix like this, so I used this approached and it worked

extension String {
func getImageFromBase64() -> UIImage? {
    guard let url = URL(string: self) else {
        return nil
    }
    do {
        let data = try Data(contentsOf: url)
        return UIImage(data: data)
    } catch {
        return nil
    }

}

}

Upvotes: 1

jlasierra
jlasierra

Reputation: 1106

This method requires padding with “=“, the length of the string must be multiple of 4.

In some implementations of base64 the padding character is not needed for decoding, since the number of missing bytes can be calculated. But in Fundation's implementation it is mandatory.

Updated: As noted on the comments, it's a good idea to check first if the string lenght is already a multiple of 4. if encoded64 has your base64 string and it's not a constant, you can do something like this:

Swift 2

let remainder = encoded64.characters.count % 4
if remainder > 0 {
    encoded64 = encoded64.stringByPaddingToLength(encoded64.characters.count + 4 - remainder,
                                                  withPad: "=",
                                                  startingAt: 0)
}

Swift 3

let remainder = encoded64.characters.count % 4
if remainder > 0 {
    encoded64 = encoded64.padding(toLength: encoded64.characters.count + 4 - remainder,
                                  withPad: "=",
                                  startingAt: 0)
}

Swift 4

let remainder = encoded64.count % 4
if remainder > 0 {
    encoded64 = encoded64.padding(toLength: encoded64.count + 4 - remainder,
                                  withPad: "=",
                                  startingAt: 0)
}

Updated one line version:

Or you can use this one line version that returns the same string when its length is already a multiple of 4:

encoded64.padding(toLength: ((encoded64.count+3)/4)*4,
                  withPad: "=",
                  startingAt: 0)

Upvotes: 58

Mohamed Elkassas
Mohamed Elkassas

Reputation: 869

(Swift 3) I been in this situation, Trying to get the data using base64 encoded string returns nil with me when I used this line

let imageData = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)

Tried padding the string and didn't work out too

This is what worked with me

func imageForBase64String(_ strBase64: String) -> UIImage? {

    do{
        let imageData = try Data(contentsOf: URL(string: strBase64)!)
        let image = UIImage(data: imageData)
        return image!
    }
    catch{
        return nil
    }
}

Upvotes: 4

Mbt925
Mbt925

Reputation: 1346

Another one-line version:

let length = encoded64.characters.count
encoded64 = encoded64.padding(toLength: length + (4 - length % 4) % 4, withPad: "=", startingAt: 0)

Upvotes: 0

k-yamada
k-yamada

Reputation: 51

When the number of characters is divisible by 4, you need to avoid padding.

private func base64PaddingWithEqual(encoded64: String) -> String {
  let remainder = encoded64.characters.count % 4
  if remainder == 0 {
    return encoded64
  } else {
    // padding with equal
    let newLength = encoded64.characters.count + (4 - remainder)
    return encoded64.stringByPaddingToLength(newLength, withString: "=", startingAtIndex: 0)
  }
}

Upvotes: 5

Related Questions