Reputation: 31
I'm trying to convert base 64 encoded string to UIImage
with the following code:
let decodedData = NSData(base64EncodedString: base64String!, options: NSDataBase64DecodingOptions(rawValue: 0) )
print(decodedData) //I get data here (It is not nil)
var decodedimage = UIImage(data: decodedData!) //return nil
The decodedData
seems fine, Why do I get nil
when converting to UIImage
?
Upvotes: 3
Views: 7503
Reputation: 1317
For Swift 4.2
if base64String != nil {
let decodedData = NSData(base64Encoded: base64String!, options: [])
if let data = decodedData {
let decodedimage = UIImage(data: data as Data)
cell.logo.image = decodedimage
} else {
print("error with decodedData")
}
} else {
print("error with base64String")
}
Upvotes: 1
Reputation: 3362
Try to pass no options, I also recommend using unwrap for optionals :
if let string = base64String {
let decodedData = NSData(base64EncodedString: base64String!, options: [])
if let data = decodedData {
var decodedimage = UIImage(data: data)
} else {
print("error with decodedData")
}
} else {
print("error with base64String")
}
Upvotes: 7