NSData转化为Base64编码的API  

2017-06-29  本文已影响50人  JACK_岩

在IOS7之后,苹果提供了将NSData转化为Base64编码的API

<span style="font-size:18px;">
/* Create an NSData from a Base-64 encoded NSString using the given options. By default, returns nil when the input is not recognized as valid Base-64.
*/

/* Create a Base-64 encoded NSString from the receiver's contents using the given options.
*/

/* Create an NSData from a Base-64, UTF-8 encoded NSData. By default, returns nil when the input is not recognized as valid Base-64.
*/

/* Create a Base-64, UTF-8 encoded NSData from the receiver's contents using the given options.
*/

其中前两个方法是用来处理字符串的,后两个方法是用来处理UTF-8编码数据的。比如如果是将编码字符串用作JSON数据,使用前两个方法,而将编码字符串写入文件,使用后两个。

对于IOS7之前的版本,以下API应该是之前就存在,只是之前为私有方法:

<span style="font-size:18px;">- (id)initWithBase64Encoding:(NSString *)base64String NS_DEPRECATED(10_6, 10_9, 4_0, 7_0);

对于IOS7之后在NSData通过Base64转化为NSString时,有一个枚举参数:NSDataBase64EncodingOptions有四个值,在将其转化为NSString时,对其进行了处理:

NSDataBase64Encoding64CharacterLineLength
其作用是将生成的Base64字符串按照64个字符长度进行等分换行。

NSDataBase64Encoding76CharacterLineLength
其作用是将生成的Base64字符串按照76个字符长度进行等分换行。

NSDataBase64EncodingEndLineWithCarriageReturn
其作用是将生成的Base64字符串以回车结束。

NSDataBase64EncodingEndLineWithLineFeed
其作用是将生成的Base64字符串以换行结束。

上一篇下一篇

猜你喜欢

热点阅读