Disclaimer: There's many questions on SO asking the same thing here. I've attempted to directly follow the solutions on those questions with no success. I've tried to avoid posting this duplicate.
Hello,
I'm having trouble decoding a base64 encoded string to an image in Swift. I've tried with a few different sets of data but they all don't seem to decode properly, as the results are always nil
.
After quite a bit of reasearch, I've concluded that the string needs to be decoded into Data
, create an image from that data and set a UIImageView
's image to it. Simple enough, right?
let base64String: String = ...
let decodedData: Data = Data(base64Encoded: base64String, options: [])
let decodedImage: UIImage = UIImage(data: decodedData)
myImageView.image = decodedImage
However, after all my various attempts, the data ends up being nil
, and I'm unable to create the image.
I have a feeling it might be the data that I'm using, but online conversion tools seem to have no problem using it. See below for a sample of my data and a website that I use for conversion.