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];

相关推荐
m0_641031054 小时前
在选择iOS代签服务前,你必须了解的三大安全风险
ios
开开心心loky5 小时前
[iOS] push 和 present Controller 的区别
ui·ios·objective-c·cocoa
白玉cfc10 小时前
【iOS】push,pop和present,dismiss
macos·ios·cocoa
低调小一11 小时前
iOS 开发入门指南-HelloWorld
ios
2501_9159184112 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
用户8705681304517 小时前
iOS 异步渲染:从 CALayer 切入的实现与优化
ios
敲代码的鱼哇2 天前
跳转原生系统设置插件 支持安卓/iOS/鸿蒙UTS组件
android·ios·harmonyos
在下历飞雨2 天前
Kuikly基础之状态管理与数据绑定:让“孤寡”计数器动起来
ios·harmonyos
在下历飞雨2 天前
Kuikly基础之Kuikly DSL基础组件实战:构建青蛙主界面
ios·harmonyos
鹏多多.2 天前
flutter-使用fluttertoast制作丰富的高颜值toast
android·前端·flutter·ios