decodedData is nil but My base64String contains an extremely long string
decodedData是nil,但我的base64String包含一个非常长的字符串
Encode
编码
var imgProfile:NSData = UIImagePNGRepresentation(imgUI)
let base64String = imgProfile.base64EncodedStringWithOptions(.allZeros)
Decode
解码
let base64String = prefs.valueForKey("imgDefault") as? String
let decodedData = NSData(base64EncodedString: base64String!, options: NSDataBase64DecodingOptions(rawValue: 0) )
var decodedimage = UIImage(data: decodedData!)
I"m having trouble outputting my image from base64
我在用base64显示我的形象时遇到了麻烦
base64 string ENCODE before inserting into db:
在插入到db之前对base64字符串进行编码:
"iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAMAAADDpiTIAAAADFBMVEXFxcX////p6enW1tbAmiBwAAAAHGlET1QAAAACAAAAAAAAAQAAAAAoAAABAAAAAQAAAAYAppse6QAABcxJREFUeAHs3et22jAQReEQ3v+di8sikDaxZeGLdPTxpzS202jP9pwRkNWPDw8EEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQiCVwvV4/p8fl9fH3K7cjsau2sBuBqfKvVf/x+ecnDQJtKSn9qw83DQIpDLqktcV/iECCAGFuXf9Rz6o/5UHPEtTe+t9V0Qj6dGCb6t9d4EBvDrzb+b/3gOlvsqAjB97M/f+rf/+KjUEfDuxU/nsb6APBwD/llsn/UycwDTQt1443/9MGSdCqA4eUXxK0Wv6P917xed7hJc8+m6Uw7A922O1/90MOtGXaweWXA8OXnwINKXAtSe1dzvGWcQManND9nzIZBU434MjZ/1n45zP7gVMVOPX2v1ugCZxowHnp/2wBl4tJ4CwFzm7/DwvEwCkGNND+vwzQBI5XoI32/1CAAUcb0Er7fxggBg41oKH2/2WAJnCcAg3Wf/rU4HEABv+Xmqw/Aw6zsq3x7xEB0596wBEStFt/Boxefwbsb0Br27/XBJie2w7u60Dr9WfA6PVnwJ4GtDz/PbPAXmAvB/qov0lw9PozYB8Dern/pyyQAts70Ojrv8/of33mfYHNBeiq/t4X2L
“iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAMAAADDpiTIAAAADFBMVEXFxcX / / / / p6enW1tbAmiBwAAAAHGlET1QAAAACAAAAAAAAAQAAAAAoAAABAAAAAQAAAAYAppse6QAABcxJREFUeAHs3et22jAQReEQ3v + di8sikDaxZeGLdPTxpzS202jP9pwRkNWPDw8EEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQiCVwvV4 / p8fl9fH3K7cjsau2sBuBqfKvVf / x + ecnDQJtKSn9qw83DQIpDLqktcV / iECCAGFuXf9Rz6o / 5 uhpette + t9V0Qj6dGCb6t9d4EBvDrzb + b / 3 golvsqajb97m / f + rf / + KjUEfDuxU / nsb6APBwD llsn / UycwDTQt1443/9MGSdCqA4eUXxK0Wv6P917xed7hJc8 + m6Uw7A922O1/90MOtGXaweWXA8OXnwINKXAtSe1dzvGWcQManND9nzIZBU434MjZ / 1 n45zp7gvmvopx2v1ugczxowhnp / 2 wbl4tj4cwfzm7 / DwvEwCkGNND + vwzQBI5XoI32/1CAAUcb0Er7fxggBg41oKH2/2WAJnCcAg3Wf / rU4HEABv + Xmqw / Aw6zsq3x7xEB0596wBEStFt / Boxefwbsb0Br27 / XBJie2w7u60Dr9WfA6PVnwJ4GtDz / PbPAXmAvB / qov0lw9PozYB8Dern / pyyQAts70Ojrv8 / of33mfYHNBeiq / t4X2L
base64 string DECODE when pulling down from db:
从db拉下时的base64字符串解码:
Optional("iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAMAAADDpiTIAAAADFBMVEXFxcX////p6enW1tbAmiBwAAAAHGlET1QAAAACAAAAAAAAAQAAAAAoAAABAAAAAQAAAAYAppse6QAABcxJREFUeAHs3et22jAQReEQ3v di8sikDaxZeGLdPTxpzS202jP9pwRkNWPDw8EEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQiCVwvV4/p8fl9fH3K7cjsau2sBuBqfKvVf/x ecnDQJtKSn9qw83DQIpDLqktcV/iECCAGFuXf9Rz6o/5UHPEtTe t9V0Qj6dGCb6t9d4EBvDrzb b/3gOlvsqAjB97M/f rf/ KjUEfDuxU/nsb6APBwD/llsn/UycwDTQt1443/9MGSdCqA4eUXxK0Wv6P917xed7hJc8 m6Uw7A922O1/90MOtGXaweWXA8OXnwINKXAtSe1dzvGWcQManND9nzIZBU434MjZ/1n45zP7gVMVOPX2v1ugCZxowHnp/2wBl4tJ4CwFzm7/DwvEwCkGNND vwzQBI5XoI32/1CAAUcb0Er7fxggBg41oKH2/2WAJnCcAg3Wf/rU4HEABv Xmqw/Aw6zsq3x7xEB0596wBEStFt/Boxefwbsb0Br27/XBJie2w7u60Dr9WfA6PVnwJ4GtDz/PbPAXmAvB/qov0lw9PozYB8Dern/pyyQAts70Ojrv8/of33mfYHNB
可选(“iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAMAAADDpiTIAAAADFBMVEXFxcX / / / / p6enW1tbAmiBwAAAAHGlET1QAAAACAAAAAAAAAQAAAAAoAAABAAAAAQAAAAYAppse6QAABcxJREFUeAHs3et22jAQReEQ3v,,,,,,,,,,,,,,,,,KjUEfDuxU / nsb6APBwD / llsn / UycwDTQt1443/9MGSdCqA4eUXxK0Wv6P917xed7hJc8 m6Uw7A922O1/90MOtGXaweWXA8OXnwINKXAtSe1dzvGWcQManND9nzIZBU434MjZ / 1 n45zp7gvmvopx2v1ugczxowhnp / 2 wbl4tj4cwfzm7 / DwvEwCkGNND vwzQBI5XoI32/1CAAUcb0Er7fxggBg41oKH2/2WAJnCcAg3Wf / rU4HEABv Xmqw / Aw6zsq3x7xEB0596wBEStFt Boxefwbsb0Br27 / XBJie2w7u60Dr9WfA6PVnwJ4GtDz / PbPAXmAvB / qov0lw9PozYB8Dern / pyyQAts70Ojrv8 / of33mfYHNB
2 个解决方案
#1
2
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.看起来+字符已经被空格替换了。如果您提交一个应用程序/x-www-form- urlencoding请求,而不转义+字符的百分比,就会发生这种情况。这可能发生在您第一次发送base64字符串存储在数据库中时。
See https://*.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.请参阅https://*.com/a/24888789/1271826,了解有关百分比编码模式的讨论。这里的关键是不要依赖于stringbyaddpercent来逃避usingencoding,因为它允许+字符不转义。
-
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.字符串还缺少结尾=字符。(字符串的长度应该是4的倍数,在这种情况下,它是两个字符短,因此在呈现结束时应该有==,其中包含+字符(“before”字符串)。尽管这有时是设计糟糕的base64编码器所犯的错误,但这并不是base64EncodedStringWithOptions所面临的问题。
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.
在这种情况下,看起来一个长得多的base64字符串一定被以某种方式截断了。(您的字符串可能接近1024个字符。lol)。如果将参数放在URL中而不是请求的主体中,就可能发生这种截断。但是在这个代码示例中没有任何东西可以解释这种行为,所以问题在于其他地方。
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).但是看看原始NSData的长度。base64字符串应该比这个大1/3(加上后面的四个字符,一旦包含了尾随=字符)。
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.)一旦你解码了你提供的字符串并查看了实际的内容,你还可以看到base64字符串被截断了。(根据提供的部分,应该有1484字节的IDAT数据,而且没有IEND数据块……)不要担心这些细节,但是请放心,它基本上是说PNG数据流是不完整的。
#2
0
If you're getting nil returned, then your base64 string is not valid. NSData(base64EncodedString:options:)
requires a base64 string that is padded with = to a multiple of 4.
如果返回nil,那么base64字符串无效。NSData(base64EncodedString:options:)需要一个用=填充的base64字符串,其倍数为4。
Here's a similar issue (except in Obj-C). NSData won't accept valid base64 encoded string
这里有一个类似的问题(除了object - c)。NSData不会接受有效的base64编码字符串。
#1
2
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.看起来+字符已经被空格替换了。如果您提交一个应用程序/x-www-form- urlencoding请求,而不转义+字符的百分比,就会发生这种情况。这可能发生在您第一次发送base64字符串存储在数据库中时。
See https://*.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.请参阅https://*.com/a/24888789/1271826,了解有关百分比编码模式的讨论。这里的关键是不要依赖于stringbyaddpercent来逃避usingencoding,因为它允许+字符不转义。
-
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.字符串还缺少结尾=字符。(字符串的长度应该是4的倍数,在这种情况下,它是两个字符短,因此在呈现结束时应该有==,其中包含+字符(“before”字符串)。尽管这有时是设计糟糕的base64编码器所犯的错误,但这并不是base64EncodedStringWithOptions所面临的问题。
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.
在这种情况下,看起来一个长得多的base64字符串一定被以某种方式截断了。(您的字符串可能接近1024个字符。lol)。如果将参数放在URL中而不是请求的主体中,就可能发生这种截断。但是在这个代码示例中没有任何东西可以解释这种行为,所以问题在于其他地方。
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).但是看看原始NSData的长度。base64字符串应该比这个大1/3(加上后面的四个字符,一旦包含了尾随=字符)。
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.)一旦你解码了你提供的字符串并查看了实际的内容,你还可以看到base64字符串被截断了。(根据提供的部分,应该有1484字节的IDAT数据,而且没有IEND数据块……)不要担心这些细节,但是请放心,它基本上是说PNG数据流是不完整的。
#2
0
If you're getting nil returned, then your base64 string is not valid. NSData(base64EncodedString:options:)
requires a base64 string that is padded with = to a multiple of 4.
如果返回nil,那么base64字符串无效。NSData(base64EncodedString:options:)需要一个用=填充的base64字符串,其倍数为4。
Here's a similar issue (except in Obj-C). NSData won't accept valid base64 encoded string
这里有一个类似的问题(除了object - c)。NSData不会接受有效的base64编码字符串。