iosswiftbase64string-decoding

Error while decoding base64 string to UIImage


enter image description hereI am trying to decode base64 String to UIImage in Swift 4.2. I have tried almost every solution on stackoverflow but it's not working.

I have tried converting the string with NSData, it didn't work. Now I am converting the string to Data and afterwards to image but it's giving this error. (Type of expression is ambiguous without more context)

let encodedImageData = "gggg"
    //let imageData = NSData(base64EncodedString: encodedImageData, 
    options: .)

    //let imageData = NSData(base64Encoded: encodedImageData, 
     options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
    //let image = UIImage(data:imageData)

    //Trying to get this to work.
    if let decodedData = Data(base64Encoded:encodedImageData, 
     options: .ignoreUnknownCharacters) {
        let image = UIImage(data: decodedData)
    }

It will be much appreciated, if anyone could point out what i am doing wrong. or give me solution. i have tried most of the solution anyways. Thank you


Solution

  • So i finally found the problem. Data class was not finding the base64Encoded inializer, and xcode was getting confused. As Data class is in the Foundation Framework. so what i did was.

      if let decodedImage =  Foundation.Data(base64Encoded: 
      "your base64 String", options: .ignoreUnknownCharacters){
                let image = UIImage(data: decodedImage)
       }
    

    i directly referred it to the Foundation Framework, and it's working like a chram.