Base64把图片、文件转码成字符串(互转)
2024-10-19 02:20:41
文件Base64以后会翻倍的涨,例如一张52kb的图片 base64以后string文件大小为185kb,在通过string转回图片为135kb
图片转文字:
UIImage *_originImage = [UIImage imageNamed:@"0.jpg"]; NSData *_data = UIImageJPEGRepresentation(_originImage, 1.0f); NSString *_encodedImageStr = [_data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
[_encodedImageStr writeToFile:@"/Users/Singer/Documents/0.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSLog(@"===Encoded image:\n%@", _encodedImageStr);
文字转图片:
NSString *imageStr = [NSString stringWithContentsOfFile:@"/Users/Singer/Documents/0.txt" encoding:NSUTF8StringEncoding error:nil];
NSData *d = [[NSData alloc]initWithBase64EncodedString:imageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *image = [UIImage imageWithData:d]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
imageView.image = image;
[self.view addSubview:imageView];
[d writeToFile:@"/Users/Singer/Documents/1111.jpg" atomically:YES];
最新文章
- Breakpoint is not hit
- Websphere 系列的https证书的配置说明
- mysql cluster 名词概念解读
- JavaEE系列之(二)commons-fileupload实现文件上传、下载
- linux驱动系列之调试环境搭建一
- 转:三十三、Java图形化界面设计——布局管理器之null布局(空布局)——即SWT中的绝对布局
- 全国各城市Uber客服联系方式(电话、邮箱、微博)
- VBA -excel --遍历行
- 【LeetCode练习题】Evaluate Reverse Polish Notation
- 【BZOJ1084】最大子矩阵(动态规划)
- pandas常用函数之shift
- 使用VirtualBox把IMG文件转换为VDI文件
- 关于div容器在ie6下默认高度不为0(存在默认高度)
- HDFS客户端的权限错误:Permission denied
- 上一篇是copy了整理网上的内容,这篇是一个推荐
- VS2013编译Qt5.2.1 32位静态库debug-and-release版及结果分享
- python就业班-淘宝-目录.txt
- 【题解】Uoj79一般图最大匹配
- swift一次 Attempt to present on whose view is not in the window hierarchy的解决方法
- pandas DataFrame行或列的删除方法