gpt4 book ai didi

ios - 将字符串从 JSON 转换为 UIImage

转载 作者:行者123 更新时间:2023-11-28 13:08:33 26 4
gpt4 key购买 nike

我正在从网络服务接收一些 JSON 数据,其中包括一个表示为 Sting 的图像。如何将此字符串转换为图像?

JSON 数据在 NSDictionary 中,图像数据是“Content”-key 中的对象:

if let newBannerContentString = newBanner.objectForKey("Content") as? String {
let someImage = UIImage(contentsOfFile: newBannerContentString)
}

这会将 nil 返回给 someImage。

最佳答案

如果字符串是 base64 编码的,您可以从该字符串创建 NSData 并从该数据创建图像。

if let newBannerContentString = newBanner.objectForKey("Content") as? String {
let data = NSData(base64EncodedString: newBannerContentString, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters);
let someImage = UIImage(data: data!);
}

关于ios - 将字符串从 JSON 转换为 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32040196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com