iOS base64 转 data |图片Base64转NSData | UIImageView | UIImage

Api 接口返回 base64 图片字符串,需要显示在UIImageView 上。

假设 string类型的 base64ImageStr 为 api返回的 base64字符串

将base64字符串进行处理

objectivec 复制代码
//去除掉首尾的空白字符和换行字符
 NSString * img64 = [img stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 //进行空字符串的处理
 img64 = [img64 stringByReplacingOccurrencesOfString:@"\r" withString:@""];
 //进行换行字符串的处理
 img64 = [img64 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            
 //进行字符串转data数据
 NSData * decodedImgData = [[NSData alloc] initWithBase64EncodedString:img64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
            
//把data数据转换成图片内容
 weakSelf.picCodeImageView.image = [UIImage imageWithData:decodedImgData];

//去除掉首尾的空白字符和换行字符

NSString * img64 = base64ImageStr stringByTrimmingCharactersInSet:\[NSCharacterSet whitespaceAndNewlineCharacterSet];

//进行空字符串的处理

img64 = img64 stringByReplacingOccurrencesOfString:@"\\r" withString:@"";

//进行换行字符串的处理

img64 = img64 stringByReplacingOccurrencesOfString:@"\\n" withString:@"";

//进行字符串转data数据

NSData * decodedImgData = \[NSData alloc initWithBase64EncodedString:img64 options:NSDataBase64DecodingIgnoreUnknownCharacters];

//把data数据转换成图片内容

weakSelf.picCodeImageView.image = UIImage imageWithData:decodedImgData;

相关推荐
初级代码游戏2 天前
easy Photo Clean公测版:快速清理iPhone照片 邀请公测
ios·iphone
库奇噜啦呼2 天前
【iOS】RunLoop学习
学习·ios
黑科技iOS上架2 天前
iOS应用周末提交什么情况算卡审
经验分享·ios
zzb15802 天前
ios基础-MVC-UIView
ios·mvc·cocoa
kingbal2 天前
Flutter:Flutter SDK版本管理工具FVM
android·flutter·ios·android-studio·window
他们都不看好你,偏偏你最不争气2 天前
【iOS】Runtime - Part 2 && 消息发送:缓存、查找与转发
macos·ios·objective-c·cocoa
2501_915918413 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
他们都不看好你,偏偏你最不争气3 天前
【iOS】Runtime - Part 1 && 对象与类的本质
macos·ios·objective-c·cocoa