迅速的base64解码收益为零 [英] swift base64 decoding returns nil

查看:172
本文介绍了迅速的base64解码收益为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的code脱code Base64的字符串在迅速图像:

 让德codedData =的NSData(base64En codedString:EN codedImageData,选择:NSDataBase64DecodingOptions.IgnoreUnknownCharacters)

不幸的是,德变量codedData结果有零值

调试通过code,我验证了C $ cdImageData变量EN $不为零,是正确的EN codeD图像数据(通过使用在线的base64到图像转换器验证)。怎么可能在后面我的问题的原因?

请注意:我还是新雨燕和X code

感谢


解决方案

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

如果您的base64字符串不填充,您可以使用此:

<$p$p><$c$c>en$c$cd64.stringByPaddingToLength(en$c$cd64.characters.count+4-en$c$cd64.characters.count%4, withString:=,startingAtIndex:0)

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?

Note: I am still new to Swift and XCode

Thanks

解决方案

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

If your base64 string is not padded, you can use this:

encoded64.stringByPaddingToLength(encoded64.characters.count+4-encoded64.characters.count%4, withString: "=", startingAtIndex: 0)

这篇关于迅速的base64解码收益为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆