Search code examples
iosswiftuitableviewuiimageviewnsdata

converted image for tableview yielding "unexpectedly found nil while unwrapping optional"


When I retrieve all the information for an image from my GET request in JSON I attempt to convert the string I'm getting into an image. It comes to me as a base64 string which i convert to NSData and then I try to put it in the imageView but I keep getting an "unexpectedly found nil while unwrapping optional" error. I printed the NSData to my console before unwrapping it in my imageView line(to show it's not nil) and I get the output of

d6ae5d4c be632efe 39fa8e9e f5cfc92c 7feb391c 16f7e7e9 eb5e7e2b 0fd0e6a8 51ba97dc 865c639e 39e9ed9e  >)

it's MUCH longer but this is just to give an idea.

Here is my code where I'm getting the issue:

//Convert image here
        let base64String = media[indexPath.row].image
        let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))
        print(decodedData)

        cell.mediaImageView.image = UIImage(data: decodedData!)

Solution

  • Don't convert base64, directly convert data in image which you got from response.! hope this will help:)