0

Im trying to convert a base64 string to an UIImage

First. I decode the String as shown below

let decodedData = NSData(base64Encoded: decodeIMG, options: NSData.Base64DecodingOptions(rawValue: 0))

Then I try to convert the decoded data to an UIImage like this:

let decodedIamge = UIImage(data: decodedData as Data)

But on that line I get the following error:

Cannot convert value of type 'NSData?' to type 'Data' in coercion

I already tried using another approach to convert it by using an extension that looks like this

extension String {
//: ### Base64 encoding a string
    func base64Encoded() -> String? {
        if let data = self.data(using: .utf8) {
            return data.base64EncodedString()
        }
        return nil
    }

//: ### Base64 decoding a string
    func base64Decoded() -> String? {
        if let data = Data(base64Encoded: self) {
            return String(data: data, encoding: .utf8)
        }
        return nil
    }
}

And get this error

Incorrect argument label in call (have 'base64Encoded:', expected 'map:')

  • 1
    Why are you using `NSData` at all? Just use `Data`. – rmaddy Nov 09 '18 at 19:54
  • It used to work like that, But then I updated Xcode and got this problem So instead of: let decodedData = NSData(base64Encoded: decodeIMG, options: NSData.Base64DecodingOptions(rawValue: 0)) I should do: let decodedData = Data(base64Encoded: decodeIMG) Thanks for your reply – Jesus Rojas Nov 09 '18 at 19:56
  • String conversion to data using utf8 encoding will never return nil. `return Data(utf8).base64EncodedString()` – Leo Dabus Nov 09 '18 at 20:04
  • @LeoDabus the error in the extension comes from the decoding part : Data(base64Encoded: self) - ERROR: Incorrect argument label in call (have 'base64Encoded:', expected 'map:') Thanks anyways – Jesus Rojas Nov 09 '18 at 20:06
  • @JesusRojas try with different Data.Base64DecodingOptions – Leo Dabus Nov 09 '18 at 20:09
  • @JesusRojas if you code doesn't compile try cleaning your project. Have you tryed that in a new playground file? – Leo Dabus Nov 09 '18 at 20:12
  • this might help https://stackoverflow.com/a/36831114/2303865 – Leo Dabus Nov 09 '18 at 20:13
  • @LeoDabus I still get Incorrect argument label in call (have 'base64Encoded:', expected 'map:') when I try to use Data(base64Encoded: self) after cleaning – Jesus Rojas Nov 09 '18 at 20:17
  • @JesusRojas What is your Swift / Xcode version? – Leo Dabus Nov 09 '18 at 20:38
  • @LeoDabus Xcode 10.1 Swift 4.2 – Jesus Rojas Nov 09 '18 at 20:50
  • Have you tried that code in a new playground file? try `extension String { func base64Encoded() -> String { return Data(utf8).base64EncodedString() } func base64Decoded() -> String? { if let data = Data(base64Encoded: self, options: .ignoreUnknownCharacters) { return String(data: data, encoding: .utf8) } return nil } }` – Leo Dabus Nov 09 '18 at 20:50
  • Make sure you don't have a custom `class` or `structure` called `Data` in your project – Leo Dabus Nov 09 '18 at 20:54
  • 1
    @LeoDabus It was the structure thing but the Structure was on another file lol Thank you so much <3 – Jesus Rojas Nov 09 '18 at 20:58

0 Answers0