I am trying to decode a base64 string to an UIImage in Swift.
The encoded string for my sample image starts with:
data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKC...
The full encoded string can be seen at: base64 string
I use the following function to decode this into an image:
func ConvertBase64StringToImage (imageBase64String:String) -> UIImage {
let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
let image = UIImage(data: imageData!)
return image!
}
If I call that function with the string above as parameter, an error is occuring saying that imageData is nil (Fatal error: Unexpectedly found nil while unwrapping an Optional value).
What am I doing wrong here?