欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

图片 和 base64 互转

程序员文章站 2022-07-05 11:44:24
图片转base64 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]; UIImage *img = [UIImage imageWithData:data]; NSString *img64 = [ ......

图片转base64

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];

UIImage *img = [UIImage imageWithData:data];

NSString *img64 = [self encodeToBase64String:img];

- (NSString *)encodeToBase64String:(UIImage *)image{

NSString * test = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

return test;

}

 

base64转图片

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

UIImage *img = [UIImage imageWithData:data];