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;

相关推荐
GitLqr8 小时前
Flutter 3.47 与 Dart 3.13:你必须了解的迁移指南
flutter·ios·dart
冯汉栩13 小时前
Swift Control View,Label渐变颜色(源码)
开发语言·ios·swift
jike_202615 小时前
iPhone日历查看会议纪要APP推荐:按日期查找历史录音更方便
ios·语音识别·iphone
冯汉栩19 小时前
OC 技术 App上存蒲公英教程
ios
码云之上1 天前
小程序 iOS 多输入框焦点乱序脱坑记
ios·微信小程序·taro
2501_915909062 天前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview
随遇丿而安2 天前
第 1 周:别小看 `UILabel`,它跟 Android TextView 不只是名字不同
ios
游戏开发爱好者83 天前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
Anhty4 天前
2026 实测 4 款 AI 变声器|QQ 聊天伪装声线,告别僵硬假声
人工智能·功能测试·ios·智能手机·安卓
唔664 天前
flutter web iOS 在浏览器加载中文慢的问题
前端·flutter·ios