1

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Hashir Saeed
  • 195
  • 1
  • 10
  • 1
    The code of the `if let` expression is correct. The error must be somewhere else. Basically don't use `NS...` classes in Swift at all if there is a native equivalent. – vadian Jan 22 '19 at 11:07
  • What are the swift equivalent classes. I have used DATA later on? – Hashir Saeed Jan 22 '19 at 11:23
  • Exactly, the equivalents are the structs with the same name but without NS prefix – vadian Jan 22 '19 at 11:26
  • But it isn't working either. – Hashir Saeed Jan 22 '19 at 12:19
  • do you have a code snippet to decode base64 string to UIImage in Swift 4.2 – Hashir Saeed Jan 22 '19 at 12:20
  • I tried your code, it **does** work. If the optional binding fails then the passed string is not correctly base64 encoded (for example `gggg` will never work). But once again after reading the error message I suppose that the error is somewhere else. – vadian Jan 22 '19 at 12:24
  • Yes, i know "gggg" is not a valid base64. But the thing is. it shows a syntax error, at my side. the code dosen't even complies – Hashir Saeed Jan 22 '19 at 12:29
  • if let decodedData = NSData.init(base64Encoded: kitchen.picture!, options: []) { let data = decodedData.base64EncodedData(options: []) cell.kitchenImage.image = UIImage.init(data: data ) print( "Dcoded image: ", cell.kitchenImage.image) } – Hashir Saeed Jan 22 '19 at 12:30
  • ^ this code i wrote. it does not give any error. but it dosen't convert the string to UIImage – Hashir Saeed Jan 22 '19 at 12:31
  • What is `kitchen.picture`? – vadian Jan 22 '19 at 12:32
  • oh, it's just a base64 String – Hashir Saeed Jan 22 '19 at 12:36
  • If it's really a (non-optional) base64 string then the error message `Type of expression is ambiguous without more context)` must not appear. And please stop using `NSData` – vadian Jan 22 '19 at 12:40
  • When using data it gives me(Cannot invoke initializer for type 'Data' with an argument list of type '(base64Encoded: String, options: [Any])'). That is why i am using NSData. Data don't even have a base64Encoded inializer – Hashir Saeed Jan 22 '19 at 12:47
  • i have added a pic see – Hashir Saeed Jan 22 '19 at 12:51
  • 1
    ??? The proper initializer (`Data(base64Encoded:options:`) is in the code but the `options` type is not `[Any]`. I tried literally the code in the question. It compiles and it works (if `encodedImageData` is a valid base64 string) – vadian Jan 22 '19 at 12:51
  • Damn, i made a new project, which was on swift 4. and code works fine. – Hashir Saeed Jan 22 '19 at 13:18
  • Thanks Alot @vadian – Hashir Saeed Jan 22 '19 at 17:11

1 Answers1

0

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.

Sergio
  • 2,346
  • 2
  • 24
  • 28
Hashir Saeed
  • 195
  • 1
  • 10
  • 1
    As I said: *the error is somewhere else*. You are strongly discouraged from naming a custom class or struct `Data` or other names of the system frameworks. This avoids ambiguity errors like that. – vadian Jan 22 '19 at 17:26
  • I sure , will not forget this. it took me 9 hourse to debug this. Anyways thanks alot mate. :D – Hashir Saeed Jan 22 '19 at 17:30