-2

How do I create an image from a Base64-encoded string in Swift 3? This is what I tried:

if result.profile_image != nil && result.profile_image != ""{
    let dataDecoded:NSData = NSData(base64Encoded: result.profile_image!, options: NSData.Base64DecodingOptions.
        let decodedImage:UIImage = UIImage(data: dataDecoded as Data)!
        cell.profileImage.image = decodedImage
Donald Duck
  • 8,409
  • 22
  • 75
  • 99

2 Answers2

0

Thorough answer here, always a good idea to browse SO first.

Basically you need to create a Data instance (it has a base64Encoded: initializer), and use UIImage(data: theDataInstanceYouJustCreated) and it's there.

Community
  • 1
  • 1
michalronin
  • 243
  • 2
  • 12
-1

You can create one by wrapping your Base64 string on a Data object, then creating an image from the Data object. For instance:

if let data = Data(base64Encoded: base64) {
    let image = UIImage(data: data)
}