Swift base64 decoding returns nil
Solution 1
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)
Solution 2
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)
}
}
Solution 3
(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
}
}
Solution 4
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
}
}
}
Solution 5
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())
Related videos on Youtube
redamakarem
Updated on June 06, 2022Comments
-
redamakarem almost 2 years
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?
-
Eric Aya about 8 yearsDid you try with basic options?
let decodedData=NSData(base64EncodedString: encodedImageData, options: NSDataBase64EncodingOptions())
-
-
Mbt925 about 7 yearsfor strings already have a length of multiple of 4, your code adds four unnecessary '='!
-
jlasierra about 7 years@Mbt925 Here I am explaining to the OP why base64 decoding might be failing. Of course, it's not a bad idea to check first if your string lenght is already a multiple of 4. I take this opportunity to update to swift 3.
-
Mbt925 about 7 yearsI ended up using:
encoded64 = encoded64.padding(toLength: encoded64.characters.count + (4 - remainder) % 4, withPad: "=", startingAt: 0)
withoutif
. -
jlasierra about 7 years@Mbt925 I have updated the answer with another one line version that takes into account that the lenght can be already a multiple of 4.
-
Daniel Kanaan about 6 yearspadding with "=" seems to be broken at the moment. I suggest padding with "A", which actually works. Filed a bug already.
-
jlasierra about 6 years@DanielKanaan It works for me, with xcode 9.3 & swift 4.1. What is the bug with "="?
-
jlasierra about 6 years@DanielKanaan, be aware that 'A' is actual data, so if you use it for padding your output will have one or two extra bytes with 0.
-
Daniel Kanaan about 6 years@jlasierra
data1 = Data(base64Encoded: "C===") data2 = Data(base64Encoded: "A===") XCTAssert(data1 != data2)
-
jlasierra about 6 years@DanielKanaan This is another problem, there are some undefined bits here. Base64 for one byte with 0 is "AA=="
-
Daniel Kanaan about 6 years@jlasierra what about
data1 = Data(base64Encoded: "AA==") data2 = Data(base64Encoded: "AC==") XCTAssert(data1 != data2)
-
jlasierra about 6 years@DanielKanaan Both "AA==" and "AC==" decode to byte 0. Two characters decodes to 1 byte, with some unused bits. But extra bits should be 0. So the correct base64 for one byte with 0 is "AA==". See tools.ietf.org/html/rfc4648#page-5
-
Daniel Kanaan about 6 years@jlasierra I am just figuring out that there are basically "illegal" combinations, such as AC==. There is no bug in the base64encoded string...
-
Pawan about 5 yearsCan you please help, my code always goes in the else condition. the if condition is never true. What should I do.... The length of my String is 1132.
-
jlasierra about 5 years@Pawan, This snippet is used to pad the base64 string when needed. Length of your string is already multiple of 4. It does not need padding. You still have to decode it with
Data(base64Encoded:options:)
.