将字符串从 JSON 转换为 UIImage
Convert String from JSON to UIImage
我从网络服务接收到一些 JSON 数据,其中包括一张表示为 Sting 的图像。
如何将此字符串转换为图像?
JSON 数据在 NSDictionary
中,图像数据是对象形式的“Content
”-key:
if let newBannerContentString = newBanner.objectForKey("Content") as? String {
let someImage = UIImage(contentsOfFile: newBannerContentString)
}
这个 returns 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!);
}
我从网络服务接收到一些 JSON 数据,其中包括一张表示为 Sting 的图像。 如何将此字符串转换为图像?
JSON 数据在 NSDictionary
中,图像数据是对象形式的“Content
”-key:
if let newBannerContentString = newBanner.objectForKey("Content") as? String {
let someImage = UIImage(contentsOfFile: newBannerContentString)
}
这个 returns 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!);
}