NSData won't accept valid base64 encoded string
Your Base64 string is not valid. It must be padded with =
characters to have
a length that is a multiple of 4. In your case: "eyJlbWFp....MTM3fQ=="
.
With this padding, initWithBase64EncodedString
decodes the Base64 string correctly.
Base 64 decoding produces empty
Avoid converting to a string before decoding:
NSData *decodedData = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
Some implementations of Base64 add line breaks every 64 characters. You should be able to address this by using this option: NSDataBase64DecodingIgnoreUnknownCharacters
.
Base64 encoded string wont decode (iOS)
I used NSDataAdditions.h < see http://code.ohloh.net/file?fid=28qaXmo6xH1Z4clfmn9_wJqDqNI&cid=xVjpNPxNo_A&s=&fp=308694&mp=&projSelected=true#L0 and for .m http://code.ohloh.net/file?fid=tXQCCVHemN1iAx6ZQSy1VkBACXA&cid=xVjpNPxNo_A&s=&fp=308694&mp&projSelected=true#L0 >
NSData *imageData = [NSData dataWithBase64EncodedString:theString];
Decode base64 to uiimage iOS Swift
There are two different problems:
It would appear that the
+
characters have been replaced with spaces. That will happen if you submit anapplication/x-www-form-urlencoded
request without percent escaping the+
characters. This probably happened when you first sent the base64 string to be stored in the database.See https://stackoverflow.com/a/24888789/1271826 for a discussion of some percent encoding patterns. The key point here is to not rely upon
stringByAddingPercentEscapesUsingEncoding
, because that will allow+
characters to go unescaped.The string is also missing the trailing
=
characters. (The string's length should be a multiple of four, and in this case, it's two characters short, so there should be==
at the end of the rendition with the+
characters in it (the "before" string). While that is sometimes a mistake made by poorly designed base64-encoders, this is not a problem thatbase64EncodedStringWithOptions
suffers from.In this case, it looks like a much longer base64 string must have been truncated somehow. (Your strings are suspiciously close to 1024 characters. lol.) This truncation could happen if you put the parameters in the URL rather than the body of the request. But nothing in this code sample would account for this behavior, so the problem rests elsewhere.
But look at the
length
of the originalNSData
. The base64 string should be 1/3 larger than that (plus rounded up to the nearest four characters, once you include the trailing=
characters).And, once you decode the string that you've provided and look at the actual contents, you can also see that the base64 string was truncated. (According to the portion provided, there should be 1484 bytes of
IDAT
data, and there's not, plus there's noIEND
chunk ... don't worry about those details, but rest assured that it's basically saying that the PNG data stream is incomplete.)
How to check whether a string is Base64 encoded or not
You can use the following regular expression to check if a string constitutes a valid base64 encoding:
^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$
In base64 encoding, the character set is [A-Z, a-z, 0-9, and + /]
. If the rest length is less than 4, the string is padded with '='
characters.
^([A-Za-z0-9+/]{4})*
means the string starts with 0 or more base64 groups.
([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$
means the string ends in one of three forms: [A-Za-z0-9+/]{4}
, [A-Za-z0-9+/]{3}=
or [A-Za-z0-9+/]{2}==
.
Unable to decode UTF-8 in Obj-C in an analogous way to Java
Your string is in UTF8 and has URL encoding. You need to use NSString
's stringByRemovingPercentEncoding
method, just as in Java you are using URLDecoder
. There is no need to use an intermediate C string. HTH
Related Topics
Spritekit - Not Loading @3X Images from Sktextureatlas
Uitableview Checkmarks Disappear When Scrolling
Error: Invalid Bitcode Version (Producer: '800.0.35.0_0' Reader: '703.0.31_0')
Offscreen Uitableviewcells (For Size Calculations) Not Respecting Size Class
Popover in Swift 3 on iPhone iOS
Linker Error in iOS (Duplicate Symbols for Architecture X86_64)
Detect iOS Application About to Delete
How to Show Alertview with Activity Indicator
Using Secrandomcopybytes in Swift
Xcode 6: No Frameworks Folder in Project Navigator
Send Programmatically Sms on Jailbreak Device
iOS Protocol/Delegate Confusion
Easiest Way to Force a Crash in Swift
Right Way of Determining Internet Speed in iOS 8
Xcode 4/iOS - Send an Email Using Smtp from Inside My App
What Is Sandbox in iOS? How to Transfer Data Between One App to Another App
Crashlytics in iOS Won't Proceed Past "Build Your Project" in Fabric App