Decode a base64 data to image

Chris Mikkelsen picture Chris Mikkelsen · Dec 5, 2016 · Viewed 15.6k times · Source

This code doesn't working with Swift 3 anymore.

imageData = NSData(base64EncodedString: mediaFile, options: NSDataBase64DecodingOptions.fromRaw(0)!)

So is this one.

imageData = NSData(base64EncodedString: mediaFile, options: .allZeros)

Answer

Nirav D picture Nirav D · Dec 5, 2016

Instead of using NSData use directly Swift 3 native Data.

if let decodedData = Data(base64Encoded: mediaFile, options: .ignoreUnknownCharacters) {
    let image = UIImage(data: decodedData)
}