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

相关推荐
Magnetic_h18 小时前
【iOS】单例模式
笔记·学习·ui·ios·单例模式·objective-c
归辞...20 小时前
「iOS」——单例模式
ios·单例模式·cocoa
yanling20231 天前
黑神话悟空mac可以玩吗
macos·ios·crossove·crossove24
归辞...1 天前
「iOS」viewController的生命周期
ios·cocoa·xcode
crasowas1 天前
Flutter问题记录 - 适配Xcode 16和iOS 18
flutter·ios·xcode
2401_852403551 天前
Mac导入iPhone的照片怎么删除?快速方法讲解
macos·ios·iphone
SchneeDuan1 天前
iOS六大设计原则&&设计模式
ios·设计模式·cocoa·设计原则
JohnsonXin2 天前
【兼容性记录】video标签在 IOS 和 安卓中的问题
android·前端·css·ios·h5·兼容性
蒙娜丽宁2 天前
Go语言错误处理详解
ios·golang·go·xcode·go1.19
名字不要太长 像我这样就好2 天前
【iOS】push和pop、present和dismiss
学习·macos·ios·objective-c·cocoa