Wiingaard
Wiingaard

Reputation: 4302

Convert String from JSON to UIImage

I'm receiving some JSON data from a webservice, which includes an image represented as a Sting. How can I convert this string into an image?

The JSON data is in an NSDictionary, and the Image data is the Object form the "Content"-key:

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

This returns nil to someImage.

Upvotes: 0

Views: 1517

Answers (1)

John Tracid
John Tracid

Reputation: 4046

If string is base64 encoded you could create NSData from that string and image from that data.

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

Upvotes: 2

Related Questions