iOS中的base64加密

#import <UIKit/UIKit.h>

@interface Base64String : NSObject

+ (NSString *)base64String:(NSString*)sourceString;

+ (NSString*)encodeBase64String:(NSString*)baseString;

+ (NSString*)base64Image:(UIImage*)image;

+ (UIImage*)encodebase64WithImage:(NSString*)baseSring;

@end

/88888888888888888888888分割线88888888888888888888888888/

#import "Base64String.h"

@implementation Base64String

//把字符串加密为base64字符串

+ (NSString *)base64String:(NSString*)sourceString

{//判断字符串的长度是否小于等于0如果是,就返回空

if (sourceString.length <=0) {

return nil;

}

//把字符串转化为二进制流

NSData * sourceData = [[NSData alloc] initWithData:[sourceString dataUsingEncoding:NSUTF8StringEncoding]];

//把二进制流转化为base64字符串

NSString * baseString = [sourceData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

return baseString;

}

+ (NSString*)encodeBase64String:(NSString*)baseString

{

if (baseString.length <= 0) {

return  nil;

}

NSData * data = [[NSData alloc] initWithBase64EncodedString:baseString options:NSDataBase64DecodingIgnoreUnknownCharacters];

return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

}

+ (NSString*)base64Image:(UIImage*)image

{

if (!image) {

return nil;

}

NSData * data =  UIImagePNGRepresentation(image);

NSString * base64Str = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

return base64Str;

}

+ (UIImage*)encodebase64WithImage:(NSString*)baseSring

{

NSData * data = [[NSData alloc] initWithData:[baseSring dataUsingEncoding:NSUTF8StringEncoding]];

NSData * myData = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];

return [UIImage imageWithData:myData];

}

@end

上一篇:Splay代码简化版


下一篇:百度用AR复现朝阳门,野心渐明直指AR平台