在NSString中解码Base-64编码的PNG [英] Decode Base-64 encoded PNG in an NSString

查看:192
本文介绍了在NSString中解码Base-64编码的PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 NSData 这是Base-64编码的,我想解码它,我看到一个看起来像这样的例子

I have some NSData which is Base-64 encoded and I would like to decode it, I have seen an example that looks like this

NSData* myPNGData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

[Base64 initialize];
NSData *data = [Base64 decode:img];
cell.image.image = [UIImage imageWithData:myPNGData];

然而这给了我一大堆错误,我想知道怎么办才能得到这个工作。我需要将某种类型的文件导入到我的项目中,还是需要包含框架?

However this gives me a load of errors, I would like to know what to do in order to get this to work. Is there some type of file I need to import into my project or do I have to include a framework?

这些是我得到的错误

Use of undeclared identifier 'Base64'
Use of undeclared identifier 'Base64'
Use of undeclared identifier 'cell'

我到处寻找,无法弄清楚做什么是正确的。

I have looked everywhere and cannot figure out what is the proper thing to do.

推荐答案

NSData Base64库文件会帮助你。

#import "NSData+Base64.h"

//Data from your string is decoded & converted to UIImage
UIImage *image = [UIImage imageWithData:[NSData
dataFromBase64String:strData]];

希望有所帮助

Swift 3版本

它几乎相同

//Create your NSData object
let data = NSData(base64Encoded: "yourStringData", options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)

//And then just create a new image based on the data object
let image = UIImage(data: data as! Data)

Swift 2.3版本

//Create your NSData object
let data = NSData(base64EncodedString: "yourStringData", options: .IgnoreUnknownCharacters)

//And then just create a new image based on the data object
let image = UIImage(data: data!)

这篇关于在NSString中解码Base-64编码的PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆