菜鸟教程小白 发表于 2022-12-11 16:39:55

ios - 如何在 swift 2.0 中将图像转换为 base64 而不会丢失图像?


                                            <p><p>我尝试了以下方法,但图像无法正确显示并且丢失</p>

<pre class="lang-swift prettyprint-override"><code>func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: ) {
    img_View.image = info as? UIImage
    let image = info as! UIImage
    let imageData:NSData = UIImagePNGRepresentation(image)!
    strImage = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
    print(strImage)
    dismissViewControllerAnimated(true, completion: nil)
}
</code></pre>

<p>这里有损图像<a href="http://i.stack.imgur.com/Qhew8.png" rel="noreferrer noopener nofollow">Corrupted image here</a>
当我将图像解码到我的计算机中时,我得到了稍微模糊的完整图像,但是当我将我的 base64 字符串发送到服务(WEBAPI)(Windows PC)时,它们只得到图像的一半。有人可以帮我解决这个问题吗</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><h1> swift4</h1>
<p>这个问题有点老了,但作为谷歌搜索结果 HitTest 门的问题之一,我将为 swift4 添加解决方案。</p>
<blockquote>
<p>You need to define the option of encoding</p>
</blockquote>
<pre><code>let image = UIImage(named: &#34;yourimagename&#34;)
let imageData = UIImagePNGRepresentation(image)
let base64Str = imageData?.base64EncodedString(options: .lineLength64Characters)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 swift 2.0 中将图像转换为 base64 而不会丢失图像?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37672422/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37672422/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 swift 2.0 中将图像转换为 base64 而不会丢失图像?