IOS -- base64编码

在iOS7以后可以用NSData自带的base64EncodedStringWithOptions进行编解码:

方法如下:

- (NSString *)encodeToBase64String:(UIImage *)image 
{
 return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
  NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
  return [UIImage imageWithData:data];
}

在iOS6.1或者更低的版本中,我们用如下的方法:

增加Base64这个类到你的工程,然后:

NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
NSString *strEncoded = [Base64 encode:data];

解码

NSData* data = [Base64 decode:strEncoded ];
image.image = [UIImage imageWithData:data];
原文地址:https://www.cnblogs.com/mafeng/p/7279189.html