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

相关推荐
AL.千灯学长1 小时前
DeepSeek接入Siri(已升级支持苹果手表)完整版硅基流动DeepSeek-R1部署
人工智能·gpt·ios·ai·苹果vision pro
openinstall全渠道统计21 小时前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
早起的年轻人1 天前
Flutter CupertinoNavigationBar iOS 风格导航栏的组件
flutter·ios
貂蝉空大1 天前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app
胖虎11 天前
iOS 中的圆角与平滑圆角:从新特性到老项目适配
ios·圆角·平滑圆角·cornercurve
志飞1 天前
ios UICollectionView使用自定义UICollectionViewCell
ios·collectionview·自定义cell
Neo Evolution1 天前
Flutter与移动开发的未来:谷歌的技术愿景与实现路径
android·人工智能·学习·ios·前端框架·webview·着色器
没头脑的ht2 天前
ios App的启动过程和启动优化
ios
敲代码的鱼哇2 天前
设备唯一ID获取,支持安卓/iOS/鸿蒙Next(uni-device-id)UTS插件
android·ios·uniapp·harmonyos
江上清风山间明月2 天前
Flutter最简单的路由管理方式Navigator
android·flutter·ios·路由·页面管理·navigator