简体   繁体   中英

Error while decoding base64 string to UIImage

在此处输入图片说明 I 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM