图片 64 位编码 转换 小记


accepted

Swift

First we need to have image‘s NSData

//Use image name from bundle to create NSData
let image : UIImage = UIImage(named:"imageNameHere")!
//Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)!

//OR next possibility

//Use image‘s path to create NSData
let url:NSURL = NSURL(string : "urlHere")!
//Now use image to create into NSData format
let imageData:NSData = NSData.init(contentsOfURL: url)!

Swift 2.0 > Encoding

let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)

Swift 2.0 > Decoding

let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!

Encoding :

let strBase64 = imageData.base64EncodedStringWithOptions(.allZeros)
print(strBase64)

Decoding :

let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
print(decodedimage)
yourImageView.image = decodedimage

Objective-C

iOS7 > version

You can use NSData‘s base64EncodedStringWithOptions

Encoding :

- (NSString *)encodeToBase64String:(UIImage *)image {
 return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

Decoding :

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
  NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
  return [UIImage imageWithData:data];
}

iOS 6.1 and < version

First Option : Use this link to encode and decode image

Add Base64 class in your project.

Encoding :

 NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
 NSString *strEncoded = [Base64 encode:data];

Decoding :

 NSData* data = [Base64 decode:strEncoded ];;
 image.image = [UIImage imageWithData:data];

Another Option: Use QSUtilities for encoding and decoding

参考链接:

1.https://stackoverflow.com/questions/11251340/convert-between-uiimage-and-base64-string

2.https://matrixzk.github.io/blog/20150122/load-image-encoded-with-base64/

时间: 2024-08-18 17:19:28

图片 64 位编码 转换 小记的相关文章

C# 图片和64位编码的转换

/* 将图片转换为64位编码 */ //找到文件夹 System.IO.DirectoryInfo dd = new System.IO.DirectoryInfo("C://qq"); //得到该文件夹下所有的文件 System.IO.FileInfo[] file = dd.GetFiles(); //得到某个文件的路径 string filePath = file[1].DirectoryName + "\\" + file[1].Name; //用strea

字符串与图片的Base64编码转换操作

//图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "选择要转换的图片"; dlg.Filter = "Image files (*.jpg;*.bmp;*.gif)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";

java字符串 64位编码

byte[] encodeBase64 = Base64.encodeBase64("到了是是是是".getBytes("UTF-8")); System.out.println("RESULT: " + new String(encodeBase64)); import org.apache.commons.codec.binary.Base64;

【转】在64位机上跑32位程序注意事项

原文网址:http://blog.chinaunix.net/uid-20742320-id-4744472.html 今天在交叉编译一个应用程序时,发现porting到板子上比较麻烦(nfs还没有支持),想现在PC上调试. 于是在板子上编译好,运行,发现指定的网页打不开,以为是httpd的confi有些出入,检查了下,没有错误. 在一筹莫展之际,直接在pc上运行突然发现出现segmdefault. segmdefault又是如何出现的呢? 通过file命令查看目标文件是elf64 而reade

[百度空间] [转]将程序移植到64位Windows

from : http://goooder.bokee.com/2000373.html (雷立辉 整理) 简介:本文对如何将32位Windows程序平滑的支持和过渡到64位Windows操作系统做出了一个简单而系统的介绍.包括对于64位操作系统的版本,编程模型,一些移植原则甚至包括对驱动程序的移植原则的介绍. 作者介绍:系统分析员,现在在北京工作.作者的blog:http://goooder.blogchina.com. 前言: 或许大家还对32位的CPU及操作系统使用还是觉得非常的合乎日常需

Hadoop -2.7.2包64位编译教程

网上很多教程关于Hadoop-2.4包64位编码的教程,最新版2.7.2几乎相同,在此为大家复述一下. 分享两个比较权威的附上链接: Ubuntu用户推荐参考: http://www.aboutyun.com/forum.php?mod=viewthread&tid=8130&extra=page%3D1&page=1 CentOs系列用户参考:http://www.cnblogs.com/hadoop2015/p/4259899.html 1.前期工具准备: (1)  CentO

64位字符串的编码解码[转]

一. Base64的编码规则 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编 码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24  位数据,再把这24位数据分成4组,即每组6位.再在每组的的最高位前补两个0凑足一个字节.这样就把一个3字节为一组的数据重新编码成了4个字节.当所 要编码的数据的字节数不是3的整倍数,也就是说在分组时最后一组不够3个字节.这时在最后一组填充1到2个0字节.并在最后编码完成后在结尾添加1到2个  “=”. 例:将对ABC进

%1不是有效的 win32应用程序(64位转换32位)

phpmyadmin打开时候显示 HTTP500内部服务器错误,用火狐浏览器显示的是%1不是有效的 win32应用程序.谷歌一下,终于找出问题,原来是windows2003 64位的操作系统和32位IIS的问题..最终找到解决方法如下: 要运行 32 位版本的 ASP.NET 2.0,请按照以下步骤操作:1. 单击"开始",单击"运行",键入 cmd,然后单击"确定". 2. 键入以下命令启用 32 位模式: cscript %SYSTEMDRI

HTTP500错误:%1不是有效的 win32应用程序(64位转换32位)

IE--internet选项--高级--"显示友好的HTTP错误信息"  前面的勾取消 网站具体错误信息:%1不是有效的 win32应用程序(64位转换32位) 原因:在windows 2003 64位系统上安装完iis后,运行PHP文件提示%1不是有效的 win32应用程序,原来是windows2003  64位的操作系统和32                                       位IIS的问题. 解决办法:要运行 32 位版本的 ASP.NET 2.0 具体