<bdo id='hD1Nx'></bdo><ul id='hD1Nx'></ul>
  1. <tfoot id='hD1Nx'></tfoot>
    1. <small id='hD1Nx'></small><noframes id='hD1Nx'>

      <i id='hD1Nx'><tr id='hD1Nx'><dt id='hD1Nx'><q id='hD1Nx'><span id='hD1Nx'><b id='hD1Nx'><form id='hD1Nx'><ins id='hD1Nx'></ins><ul id='hD1Nx'></ul><sub id='hD1Nx'></sub></form><legend id='hD1Nx'></legend><bdo id='hD1Nx'><pre id='hD1Nx'><center id='hD1Nx'></center></pre></bdo></b><th id='hD1Nx'></th></span></q></dt></tr></i><div id='hD1Nx'><tfoot id='hD1Nx'></tfoot><dl id='hD1Nx'><fieldset id='hD1Nx'></fieldset></dl></div>
    2. <legend id='hD1Nx'><style id='hD1Nx'><dir id='hD1Nx'><q id='hD1Nx'></q></dir></style></legend>

      Swift base64 解码返回 nil

      时间:2024-04-14
        <bdo id='sS374'></bdo><ul id='sS374'></ul>

              <i id='sS374'><tr id='sS374'><dt id='sS374'><q id='sS374'><span id='sS374'><b id='sS374'><form id='sS374'><ins id='sS374'></ins><ul id='sS374'></ul><sub id='sS374'></sub></form><legend id='sS374'></legend><bdo id='sS374'><pre id='sS374'><center id='sS374'></center></pre></bdo></b><th id='sS374'></th></span></q></dt></tr></i><div id='sS374'><tfoot id='sS374'></tfoot><dl id='sS374'><fieldset id='sS374'></fieldset></dl></div>
                <tbody id='sS374'></tbody>

            1. <tfoot id='sS374'></tfoot>

              • <small id='sS374'></small><noframes id='sS374'>

                <legend id='sS374'><style id='sS374'><dir id='sS374'><q id='sS374'></q></dir></style></legend>
                本文介绍了Swift base64 解码返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用以下代码将 base64 字符串解码为 Swift 中的图像:

                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)
                

                不幸的是,变量 decodedData 的值为 nil

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

                通过代码调试,我验证了变量 encodedImageData 不是 nil 并且是正确的编码图像数据(通过使用在线 base64 到图像转换器进行验证).我的问题背后的原因可能是什么?

                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?

                推荐答案

                此方法需要用="填充,字符串长度必须是4的倍数.

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

                在 base64 的某些实现中,解码不需要填充字符,因为可以计算丢失字节的数量.但在 Fundation 的实施中,这是强制性的.

                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.

                更新:如评论中所述,最好先检查字符串长度是否已经是 4 的倍数.如果 encoded64 具有您的 base64 字符串并且它不是常量,则可以执行以下操作:

                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:

                斯威夫特 2

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

                斯威夫特 3

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

                斯威夫特 4

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

                更新了一行版本:

                或者你可以使用这一行版本,当它的长度已经是 4 的倍数时返回相同的字符串:

                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)
                

                这篇关于Swift base64 解码返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Android使用base64上传图片到服务器 下一篇:从 NSData 创建 base-64 字符串

                相关文章

                  1. <tfoot id='xEGHz'></tfoot>
                      <bdo id='xEGHz'></bdo><ul id='xEGHz'></ul>
                  2. <legend id='xEGHz'><style id='xEGHz'><dir id='xEGHz'><q id='xEGHz'></q></dir></style></legend>

                    <small id='xEGHz'></small><noframes id='xEGHz'>

                    <i id='xEGHz'><tr id='xEGHz'><dt id='xEGHz'><q id='xEGHz'><span id='xEGHz'><b id='xEGHz'><form id='xEGHz'><ins id='xEGHz'></ins><ul id='xEGHz'></ul><sub id='xEGHz'></sub></form><legend id='xEGHz'></legend><bdo id='xEGHz'><pre id='xEGHz'><center id='xEGHz'></center></pre></bdo></b><th id='xEGHz'></th></span></q></dt></tr></i><div id='xEGHz'><tfoot id='xEGHz'></tfoot><dl id='xEGHz'><fieldset id='xEGHz'></fieldset></dl></div>